This commit is contained in:
Kovid Goyal 2021-12-07 15:52:06 +05:30
parent 4af2526f54
commit bc25e5a8a2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 42 additions and 44 deletions

View File

@ -674,6 +674,13 @@ class Boss:
if tab: if tab:
self.confirm_tab_close(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: def confirm_tab_close(self, tab: Tab) -> None:
x = get_options().confirm_os_window_close x = get_options().confirm_os_window_close
num = tab.number_of_windows_with_running_programs if x < 0 else len(tab) 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() tm = tab.tab_manager_ref()
if tm is not None: if tm is not None:
tm.set_active_tab(tab) 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 {}' '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, 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: def handle_close_tab_confirmation(self, confirmed: bool, tab_id: int) -> None:
if data.get('response') != 'y': if not confirmed:
return return
for tab in self.all_tabs: for tab in self.all_tabs:
if tab.id == tab_id: if tab.id == tab_id:
@ -1207,16 +1213,15 @@ class Boss:
return return
if tm is not None: if tm is not None:
w = tm.active_window w = tm.active_window
self._run_kitten('ask', ['--type=yesno', '--message', _( self.confirm(
'Are you sure you want to close this OS window, it has {}' _('Are you sure you want to close this OS window, it has {}'
' windows running?').format(num)], ' windows running?').format(num),
self.handle_close_os_window_confirmation, os_window_id,
window=w, 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: def handle_close_os_window_confirmation(self, confirmed: bool, os_window_id: int) -> None:
if data.get('response') == 'y': if confirmed:
self.mark_os_window_for_close(os_window_id) self.mark_os_window_for_close(os_window_id)
else: else:
self.mark_os_window_for_close(os_window_id, NO_CLOSE_REQUESTED) 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: if current_application_quit_request() == CLOSE_BEING_CONFIRMED:
return return
assert tm is not None assert tm is not None
self._run_kitten('ask', ['--type=yesno', '--message', _( self.confirm(
'Are you sure you want to quit kitty, it has {} windows running?').format(num)], _('Are you sure you want to quit kitty, it has {} windows running?').format(num),
self.handle_quit_confirmation,
window=tm.active_window, window=tm.active_window,
custom_callback=self.handle_quit_confirmation,
default_data={'response': 'n'}
) )
set_application_quit_request(CLOSE_BEING_CONFIRMED) set_application_quit_request(CLOSE_BEING_CONFIRMED)
def handle_quit_confirmation(self, data: Dict[str, Any], *a: Any) -> None: def handle_quit_confirmation(self, confirmed: bool) -> None:
set_application_quit_request(IMPERATIVE_CLOSE_REQUESTED if data.get('response') == 'y' else NO_CLOSE_REQUESTED) set_application_quit_request(IMPERATIVE_CLOSE_REQUESTED if confirmed else NO_CLOSE_REQUESTED)
def notify_on_os_window_death(self, address: str) -> None: def notify_on_os_window_death(self, address: str) -> None:
import socket import socket

View File

@ -1049,23 +1049,21 @@ class FileTransmission:
def start_send(self, asd_id: str) -> None: def start_send(self, asd_id: str) -> None:
asd = self.active_sends[asd_id] asd = self.active_sends[asd_id]
if asd.bypass_ok is not None: 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 return
boss = get_boss() boss = get_boss()
window = boss.window_id_map.get(self.window_id) window = boss.window_id_map.get(self.window_id)
if window is not None: if window is not None:
boss._run_kitten('ask', ['--type=yesno', '--message', _( boss.confirm(_(
'The remote machine wants to read some files from this computer. Do you want to allow the transfer?' '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,
window=window, custom_callback=partial(self.handle_receive_confirmation, asd_id),
default_data={'response': 'n'}
) )
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) asd = self.active_sends.get(cmd_id)
if asd is None: if asd is None:
return return
if data.get('response') == 'y': if confirmed:
asd.accepted = True asd.accepted = True
else: else:
self.drop_send(asd.id) self.drop_send(asd.id)
@ -1081,23 +1079,21 @@ class FileTransmission:
def start_receive(self, ar_id: str) -> None: def start_receive(self, ar_id: str) -> None:
ar = self.active_receives[ar_id] ar = self.active_receives[ar_id]
if ar.bypass_ok is not None: 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 return
boss = get_boss() boss = get_boss()
window = boss.window_id_map.get(self.window_id) window = boss.window_id_map.get(self.window_id)
if window is not None: if window is not None:
boss._run_kitten('ask', ['--type=yesno', '--message', _( boss.confirm(_(
'The remote machine wants to send some files to this computer. Do you want to allow the transfer?' '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,
window=window, custom_callback=partial(self.handle_send_confirmation, ar_id),
default_data={'response': 'n'}
) )
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) ar = self.active_receives.get(cmd_id)
if ar is None: if ar is None:
return return
if data.get('response') == 'y': if confirmed:
ar.accepted = True ar.accepted = True
else: else:
self.drop_receive(ar.id) self.drop_receive(ar.id)
@ -1130,10 +1126,10 @@ class TestFileTransmission(FileTransmission):
return True return True
def start_receive(self, aid: str) -> None: 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: 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]: def callback_after(self, callback: Callable[[Optional[int]], None], timeout: float = 0) -> Optional[int]:
callback(None) callback(None)

View File

@ -909,19 +909,17 @@ class Window:
self.current_clipboard_read_ask = primary self.current_clipboard_read_ask = primary
return return
self.current_clipboard_read_ask = primary 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.' 'A program running in this window wants to read from the system clipboard.'
' Allow it do so, once?')], ' Allow it do so, once?'),
window=self, self.handle_clipboard_confirmation, window=self,
custom_callback=self.handle_clipboard_confirmation,
default_data={'response': 'n'}
) )
def handle_clipboard_confirmation(self, data: Dict[str, Any], *a: Any) -> None: def handle_clipboard_confirmation(self, confirmed: bool) -> None:
try: try:
loc = 'p' if self.current_clipboard_read_ask else 'c' loc = 'p' if self.current_clipboard_read_ask else 'c'
response = '' response = ''
if data.get('response') == 'y': if confirmed:
response = get_primary_selection() if self.current_clipboard_read_ask else get_clipboard_string() response = get_primary_selection() if self.current_clipboard_read_ask else get_clipboard_string()
self.send_osc52(loc, response) self.send_osc52(loc, response)
finally: finally: