1from typing import TYPE_CHECKING
2
3from kivy.factory import Factory
4from kivy.lang import Builder
5from kivy.core.clipboard import Clipboard
6from kivy.app import App
7from kivy.clock import Clock
8
9from electrum.gui.kivy.i18n import _
10
11if TYPE_CHECKING:
12    from ...main_window import ElectrumWindow
13
14
15Builder.load_string('''
16#:import KIVY_GUI_PATH electrum.gui.kivy.KIVY_GUI_PATH
17
18<QRDialog@Popup>
19    id: popup
20    title: ''
21    data: ''
22    shaded: False
23    help_text: ''
24    close_button_text: ''
25    AnchorLayout:
26        anchor_x: 'center'
27        BoxLayout:
28            orientation: 'vertical'
29            size_hint: 1, 1
30            padding: '10dp'
31            spacing: '10dp'
32            QRCodeWidget:
33                id: qr
34                shaded: False
35                foreground_color: (0, 0, 0, 0.5) if self.shaded else (0, 0, 0, 0)
36                on_touch_down:
37                    touch = args[1]
38                    if self.collide_point(*touch.pos): self.shaded = not self.shaded
39            TopLabel:
40                text: root.help_text
41            Widget:
42                size_hint: 1, 0.2
43            BoxLayout:
44                size_hint: 1, None
45                height: '48dp'
46                Button:
47                    size_hint: 1, None
48                    height: '48dp'
49                    text: _('Copy')
50                    on_release:
51                        root.copy_to_clipboard()
52                IconButton:
53                    icon: f'atlas://{KIVY_GUI_PATH}/theming/atlas/light/share'
54                    size_hint: 0.6, None
55                    height: '48dp'
56                    on_release: root.do_share()
57                Button:
58                    size_hint: 1, None
59                    height: '48dp'
60                    text: root.close_button_text
61                    on_release:
62                        popup.dismiss()
63                        if root.on_close: root.on_close()
64''')
65
66class QRDialog(Factory.Popup):
67
68    def __init__(
69            self, title, data, show_text, *,
70            failure_cb=None,
71            text_for_clipboard=None,
72            help_text=None,
73            close_button_text=None,
74            on_close=None):
75
76        Factory.Popup.__init__(self)
77        self.app = App.get_running_app()  # type: ElectrumWindow
78        self.title = title
79        self.data = data
80        self.help_text = (data if show_text else help_text) or ''
81        self.failure_cb = failure_cb
82        self.text_for_clipboard = text_for_clipboard if text_for_clipboard else data
83        self.close_button_text = close_button_text or _('Close')
84        self.on_close = on_close
85
86    def on_open(self):
87        self.ids.qr.set_data(self.data, self.failure_cb)
88
89    def copy_to_clipboard(self):
90        Clipboard.copy(self.text_for_clipboard)
91        msg = _('Text copied to clipboard.')
92        Clock.schedule_once(lambda dt: self.app.show_info(msg))
93
94    def do_share(self):
95        self.app.do_share(self.text_for_clipboard, self.title)
96        self.dismiss()
97