diff --git a/kitty/boss.py b/kitty/boss.py index 8a33e0766..1f6df2b2d 100755 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -674,6 +674,13 @@ class Boss: if tab: self.confirm_tab_close(tab) + def confirm(self, msg: str, callback: Callable[..., None], *args: Any, + window: Optional[Window] = None, confirm_on_cancel: bool = False) -> None: + def callback_(res: Dict[str, Any], x: int, boss: Boss) -> None: + callback(res.get('response') == 'y', *args) + self._run_kitten('ask', ['--type=yesno', '--message', msg], + window=window, custom_callback=callback_, default_data={'response': 'y' if confirm_on_cancel else 'n'}) + def confirm_tab_close(self, tab: Tab) -> None: x = get_options().confirm_os_window_close num = tab.number_of_windows_with_running_programs if x < 0 else len(tab) @@ -685,16 +692,15 @@ class Boss: tm = tab.tab_manager_ref() if tm is not None: tm.set_active_tab(tab) - self._run_kitten('ask', ['--type=yesno', '--message', _( + self.confirm(_( 'Are you sure you want to close this tab, it has {}' - ' windows running?').format(num)], + ' windows running?').format(num), + self.handle_close_tab_confirmation, tab.id, window=tab.active_window, - custom_callback=partial(self.handle_close_tab_confirmation, tab.id), - default_data={'response': 'n'} ) - def handle_close_tab_confirmation(self, tab_id: int, data: Dict[str, Any], *a: Any) -> None: - if data.get('response') != 'y': + def handle_close_tab_confirmation(self, confirmed: bool, tab_id: int) -> None: + if not confirmed: return for tab in self.all_tabs: if tab.id == tab_id: @@ -1207,16 +1213,15 @@ class Boss: return if tm is not None: w = tm.active_window - self._run_kitten('ask', ['--type=yesno', '--message', _( - 'Are you sure you want to close this OS window, it has {}' - ' windows running?').format(num)], + self.confirm( + _('Are you sure you want to close this OS window, it has {}' + ' windows running?').format(num), + self.handle_close_os_window_confirmation, os_window_id, window=w, - custom_callback=partial(self.handle_close_os_window_confirmation, os_window_id), - default_data={'response': 'n'} ) - def handle_close_os_window_confirmation(self, os_window_id: int, data: Dict[str, Any], *a: Any) -> None: - if data.get('response') == 'y': + def handle_close_os_window_confirmation(self, confirmed: bool, os_window_id: int) -> None: + if confirmed: self.mark_os_window_for_close(os_window_id) else: self.mark_os_window_for_close(os_window_id, NO_CLOSE_REQUESTED) @@ -1248,16 +1253,15 @@ class Boss: if current_application_quit_request() == CLOSE_BEING_CONFIRMED: return assert tm is not None - self._run_kitten('ask', ['--type=yesno', '--message', _( - 'Are you sure you want to quit kitty, it has {} windows running?').format(num)], + self.confirm( + _('Are you sure you want to quit kitty, it has {} windows running?').format(num), + self.handle_quit_confirmation, window=tm.active_window, - custom_callback=self.handle_quit_confirmation, - default_data={'response': 'n'} ) set_application_quit_request(CLOSE_BEING_CONFIRMED) - def handle_quit_confirmation(self, data: Dict[str, Any], *a: Any) -> None: - set_application_quit_request(IMPERATIVE_CLOSE_REQUESTED if data.get('response') == 'y' else NO_CLOSE_REQUESTED) + def handle_quit_confirmation(self, confirmed: bool) -> None: + set_application_quit_request(IMPERATIVE_CLOSE_REQUESTED if confirmed else NO_CLOSE_REQUESTED) def notify_on_os_window_death(self, address: str) -> None: import socket diff --git a/kitty/file_transmission.py b/kitty/file_transmission.py index 9cead0df2..b585d5728 100644 --- a/kitty/file_transmission.py +++ b/kitty/file_transmission.py @@ -1049,23 +1049,21 @@ class FileTransmission: def start_send(self, asd_id: str) -> None: asd = self.active_sends[asd_id] if asd.bypass_ok is not None: - self.handle_receive_confirmation(asd_id, {'response': 'y' if asd.bypass_ok else 'n'}) + self.handle_receive_confirmation(asd.bypass_ok, asd_id) return boss = get_boss() window = boss.window_id_map.get(self.window_id) if window is not None: - boss._run_kitten('ask', ['--type=yesno', '--message', _( - 'The remote machine wants to read some files from this computer. Do you want to allow the transfer?' - )], - window=window, custom_callback=partial(self.handle_receive_confirmation, asd_id), - default_data={'response': 'n'} + boss.confirm(_( + 'The remote machine wants to read some files from this computer. Do you want to allow the transfer?'), + self.handle_receive_confirmation, asd_id, window=window, ) - def handle_receive_confirmation(self, cmd_id: str, data: Dict[str, str], *a: Any) -> None: + def handle_receive_confirmation(self, confirmed: bool, cmd_id: str) -> None: asd = self.active_sends.get(cmd_id) if asd is None: return - if data.get('response') == 'y': + if confirmed: asd.accepted = True else: self.drop_send(asd.id) @@ -1081,23 +1079,21 @@ class FileTransmission: def start_receive(self, ar_id: str) -> None: ar = self.active_receives[ar_id] if ar.bypass_ok is not None: - self.handle_send_confirmation(ar_id, {'response': 'y' if ar.bypass_ok else 'n'}) + self.handle_send_confirmation(ar.bypass_ok, ar_id) return boss = get_boss() window = boss.window_id_map.get(self.window_id) if window is not None: - boss._run_kitten('ask', ['--type=yesno', '--message', _( - 'The remote machine wants to send some files to this computer. Do you want to allow the transfer?' - )], - window=window, custom_callback=partial(self.handle_send_confirmation, ar_id), - default_data={'response': 'n'} + boss.confirm(_( + 'The remote machine wants to send some files to this computer. Do you want to allow the transfer?'), + self.handle_send_confirmation, ar_id, window=window, ) - def handle_send_confirmation(self, cmd_id: str, data: Dict[str, str], *a: Any) -> None: + def handle_send_confirmation(self, confirmed: bool, cmd_id: str) -> None: ar = self.active_receives.get(cmd_id) if ar is None: return - if data.get('response') == 'y': + if confirmed: ar.accepted = True else: self.drop_receive(ar.id) @@ -1130,10 +1126,10 @@ class TestFileTransmission(FileTransmission): return True def start_receive(self, aid: str) -> None: - self.handle_send_confirmation(aid, {'response': 'y' if self.allow else 'n'}) + self.handle_send_confirmation(self.allow, aid) def start_send(self, aid: str) -> None: - self.handle_receive_confirmation(aid, {'response': 'y' if self.allow else 'n'}) + self.handle_receive_confirmation(self.allow, aid) def callback_after(self, callback: Callable[[Optional[int]], None], timeout: float = 0) -> Optional[int]: callback(None) diff --git a/kitty/window.py b/kitty/window.py index dfd39b5ee..17fc05c90 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -909,19 +909,17 @@ class Window: self.current_clipboard_read_ask = primary return self.current_clipboard_read_ask = primary - get_boss()._run_kitten('ask', ['--type=yesno', '--message', _( + get_boss().confirm(_( 'A program running in this window wants to read from the system clipboard.' - ' Allow it do so, once?')], - window=self, - custom_callback=self.handle_clipboard_confirmation, - default_data={'response': 'n'} + ' Allow it do so, once?'), + self.handle_clipboard_confirmation, window=self, ) - def handle_clipboard_confirmation(self, data: Dict[str, Any], *a: Any) -> None: + def handle_clipboard_confirmation(self, confirmed: bool) -> None: try: loc = 'p' if self.current_clipboard_read_ask else 'c' response = '' - if data.get('response') == 'y': + if confirmed: response = get_primary_selection() if self.current_clipboard_read_ask else get_clipboard_string() self.send_osc52(loc, response) finally: