soulseb

poiskipopi

Jun 11th, 2025 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.19 KB | None | 0 0
  1. from kivy.config import Config
  2. Config.set('graphics', 'width', '360')
  3. Config.set('graphics', 'height', '640')
  4. Config.set('graphics', 'resizable', '0')
  5.  
  6. from kivy.app import App
  7. from kivy.lang import Builder
  8. from kivy.animation import Animation
  9. from kivy.core.window import Window
  10. from kivy.metrics import dp
  11. from kivy.uix.boxlayout import BoxLayout
  12. from kivy.uix.button import Button
  13. from kivy.uix.widget import Widget
  14. from kivy.uix.behaviors import ButtonBehavior
  15. from kivy.uix.label import Label
  16. from kivy.properties import StringProperty
  17. from kivy.utils import platform
  18.  
  19. Builder.load_string('''
  20. <ClickableArea>:
  21.    canvas:
  22.        Color:
  23.            rgba: 0, 0, 0, 0
  24.        Rectangle:
  25.            pos: self.pos
  26.            size: self.size
  27.  
  28. <ContentButton>:
  29.    orientation: 'horizontal'
  30.    size_hint_y: None
  31.    height: dp(50)
  32.    spacing: dp(5)
  33.    padding: dp(5)
  34.    canvas.before:
  35.        Color:
  36.            rgba: 0.4, 0.4, 0.6, 1
  37.        Rectangle:
  38.            pos: self.pos
  39.            size: self.size
  40.    
  41.    Label:
  42.        id: main_label
  43.        text: root.main_text
  44.        size_hint_x: 0.8
  45.        halign: 'left'
  46.        valign: 'middle'
  47.        text_size: self.width, None
  48.        color: 1, 1, 1, 1
  49.        font_size: dp(16)
  50.        bold: True
  51.    
  52.    Button:
  53.        id: info_button
  54.        text: 'i'
  55.        size_hint_x: 0.2
  56.        size_hint_y: 1
  57.        background_normal: ''
  58.        background_color: 0.8, 0.8, 0.2, 1
  59.        font_size: dp(14)
  60.        bold: True
  61.        on_press: root.on_info_press()
  62.  
  63. <SearchAppUI>:
  64.    orientation: 'vertical'
  65.    spacing: dp(7)
  66.    
  67.    # Плашка результатов
  68.    BoxLayout:
  69.        id: result_panel
  70.        size_hint_y: None
  71.        height: 0
  72.        opacity: 0
  73.        canvas.before:
  74.            Color:
  75.                rgba: 0.2, 0.8, 0.4, 0.9
  76.            Rectangle:
  77.                pos: self.pos
  78.                size: self.size
  79.        Label:
  80.            id: result_label
  81.            text: ''
  82.            color: 1, 1, 1, 1
  83.            font_size: dp(18)
  84.            bold: True
  85.            halign: 'center'
  86.            padding: [dp(10), dp(5)]
  87.    
  88.    # Основная область контента с кнопками
  89.    ScrollView:
  90.        id: main_scroll
  91.        size_hint_y: 1
  92.        do_scroll_y: True
  93.        
  94.        BoxLayout:
  95.            id: content_box
  96.            orientation: 'vertical'
  97.            size_hint_y: None
  98.            height: self.minimum_height
  99.            spacing: dp(5)
  100.            padding: [dp(10), dp(70)]
  101.    
  102.    # Поисковая панель (фиксированная внизу)
  103.    BoxLayout:
  104.        id: search_panel
  105.        size_hint_y: None
  106.        height: dp(60)
  107.        padding: dp(10)
  108.        spacing: dp(5)
  109.        
  110.        TextInput:
  111.            id: search_input
  112.            hint_text: 'Введите запрос...'
  113.            multiline: False
  114.            size_hint_x: 0.8
  115.            padding: [dp(10), (self.height - self.line_height)/2]
  116.            font_size: dp(16)
  117.            on_focus: root.on_search_focus(*args)
  118.            on_text_validate: root.do_search()
  119.        
  120.        Button:
  121.            id: action_button
  122.            text: 'Добавить'
  123.            size_hint_x: 0.2
  124.            font_size: dp(13)
  125.            bold: True
  126.            background_normal: ''
  127.            background_color: (0.4, 0.6, 0.4, 1)
  128.            on_press: root.toggle_result_panel()
  129.    
  130.    # Невидимая кликабельная область (над клавиатурой)
  131.    ClickableArea:
  132.        id: clickable_area
  133.        size_hint_y: None
  134.        height: 0
  135.        on_press: root.hide_keyboard()
  136. ''')
  137.  
  138. class ClickableArea(ButtonBehavior, Widget):
  139.     pass
  140.  
  141. class ContentButton(ButtonBehavior, BoxLayout):
  142.     main_text = StringProperty('')
  143.     info_text = StringProperty('')
  144.    
  145.     def __init__(self, main_text="", info_text="", **kwargs):
  146.         super().__init__(**kwargs)
  147.         self.main_text = main_text
  148.         self.info_text = info_text
  149.    
  150.     def on_press(self):
  151.         app = App.get_running_app()
  152.         app.root.ids.result_label.text = f"Выбрано: {self.main_text}"
  153.         if app.root.ids.result_panel.height == 0:
  154.             app.root.show_result_panel()
  155.    
  156.     def on_info_press(self):
  157.         app = App.get_running_app()
  158.         app.root.ids.result_label.text = f"Инфо: {self.info_text}"
  159.         if app.root.ids.result_panel.height == 0:
  160.             app.root.show_result_panel()
  161.  
  162. class SearchAppUI(BoxLayout):
  163.     def __init__(self, **kwargs):
  164.         super().__init__(**kwargs)
  165.         Window.bind(on_keyboard=self.on_keyboard)
  166.         self.keyboard_height = dp(300)
  167.         self.create_content_buttons()
  168.        
  169.         if platform == 'android':
  170.             from android.runnable import run_on_ui_thread
  171.             from jnius import autoclass
  172.             self._activity = autoclass('org.kivy.android.PythonActivity').mActivity
  173.             Window.bind(on_keyboard=self.on_keyboard)
  174.    
  175.     def on_keyboard(self, window, key, *args):
  176.         if key == 27:
  177.             if self.ids.search_input.focus:
  178.                 self.hide_keyboard()
  179.                 return True
  180.             return False
  181.    
  182.     def create_content_buttons(self):
  183.         content_box = self.ids.content_box
  184.         for i in range(1, 21):
  185.             btn = ContentButton(
  186.                 main_text=f'Элемент {i}',
  187.                 info_text=f'Доп. информация {i}'
  188.             )
  189.             content_box.add_widget(btn)
  190.    
  191.     def on_search_focus(self, instance, focused):
  192.         if focused:
  193.             self.ids.clickable_area.height = self.keyboard_height
  194.         else:
  195.             self.ids.clickable_area.height = 0
  196.    
  197.     def hide_keyboard(self):
  198.         self.ids.search_input.focus = False
  199.    
  200.     def toggle_result_panel(self):
  201.         if self.ids.result_panel.height == 0:
  202.             query = self.ids.search_input.text.strip()
  203.             if query:
  204.                 self.ids.result_label.text = f"Добавлено: {query}"
  205.             else:
  206.                 self.ids.result_label.text = "Введите текст для добавления"
  207.             self.show_result_panel()
  208.         else:
  209.             self.hide_result_panel()
  210.    
  211.     def show_result_panel(self):
  212.         Animation(height=dp(50), opacity=1, duration=0.3).start(self.ids.result_panel)
  213.         self.ids.action_button.text = "Убрать"
  214.         self.ids.action_button.background_color = (0.8, 0.3, 0.3, 1)
  215.    
  216.     def hide_result_panel(self):
  217.         Animation(height=0, opacity=0, duration=0.3).start(self.ids.result_panel)
  218.         self.ids.action_button.text = "Добавить"
  219.         self.ids.action_button.background_color = (0.4, 0.6, 0.4, 1)
  220.    
  221.     def do_search(self):
  222.         query = self.ids.search_input.text.strip()
  223.         if query:
  224.             self.ids.result_label.text = f"Найдено: {query}"
  225.             if self.ids.result_panel.height == 0:
  226.                 self.show_result_panel()
  227.  
  228. class SearchApp(App):
  229.     def build(self):
  230.         Window.clearcolor = (0.4, 0.4, 0.4, 1)
  231.         return SearchAppUI()
  232.  
  233. if __name__ == '__main__':
  234.     SearchApp().run()
Add Comment
Please, Sign In to add comment