-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_bar.py
More file actions
69 lines (51 loc) · 1.94 KB
/
search_bar.py
File metadata and controls
69 lines (51 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from abc import ABC, abstractmethod
from .utils import IS_ANDROID
from .view import ViewBase
# ========================================
# Base class
# ========================================
class SearchBarBase(ABC):
@abstractmethod
def __init__(self) -> None:
super().__init__()
@abstractmethod
def set_query(self, query: str) -> "SearchBarBase":
pass
@abstractmethod
def get_query(self) -> str:
pass
if IS_ANDROID:
# ========================================
# Android class
# https://developer.android.com/reference/android/widget/SearchView
# ========================================
from typing import Any
from java import jclass
class SearchBar(SearchBarBase, ViewBase):
def __init__(self, context: Any, query: str = "") -> None:
super().__init__()
self.native_class = jclass("android.widget.SearchView")
self.native_instance = self.native_class(context)
self.set_query(query)
def set_query(self, query: str) -> "SearchBar":
self.native_instance.setQuery(query, False)
return self
def get_query(self) -> str:
return self.native_instance.getQuery().toString()
else:
# ========================================
# iOS class
# https://developer.apple.com/documentation/uikit/uisearchbar
# ========================================
from rubicon.objc import ObjCClass
class SearchBar(SearchBarBase, ViewBase):
def __init__(self, query: str = "") -> None:
super().__init__()
self.native_class = ObjCClass("UISearchBar")
self.native_instance = self.native_class.alloc().init()
self.set_query(query)
def set_query(self, query: str) -> "SearchBar":
self.native_instance.set_text_(query)
return self
def get_query(self) -> str:
return self.native_instance.text()