Refactor NewConfirmationDialog

This commit is contained in:
Philipp Hörist 2019-03-26 20:14:53 +01:00
parent 1551ffa0b4
commit 21aa352a34
3 changed files with 57 additions and 24 deletions

View File

@ -6,9 +6,6 @@ from gajim.common.i18n import _
Option = namedtuple('Option', 'kind label type value name callback data desc enabledif props') Option = namedtuple('Option', 'kind label type value name callback data desc enabledif props')
Option.__new__.__defaults__ = (None,) * len(Option._fields) # type: ignore Option.__new__.__defaults__ = (None,) * len(Option._fields) # type: ignore
DialogButton = namedtuple('DialogButton', 'text callback action')
DialogButton.__new__.__defaults__ = (None, None) # type: ignore
EncryptionData = namedtuple('EncryptionData', 'additional_data') EncryptionData = namedtuple('EncryptionData', 'additional_data')
EncryptionData.__new__.__defaults__ = (None,) # type: ignore EncryptionData.__new__.__defaults__ = (None,) # type: ignore

View File

@ -12,6 +12,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>. # along with Gajim. If not, see <http://www.gnu.org/licenses/>.
from collections import namedtuple
from gi.repository import Gtk from gi.repository import Gtk
from gajim.common import app from gajim.common import app
@ -22,6 +24,36 @@ from gajim.gtk.util import get_builder
from gajim.gtk.util import load_icon from gajim.gtk.util import load_icon
class DialogButton(namedtuple('DialogButton', ('response text callback args '
'kwargs action is_default'))):
@classmethod
def make(cls, type_=None, **kwargs):
# Defaults
default_kwargs = {
'response': None,
'text': None,
'callback': None,
'args': [],
'kwargs': {},
'action': None,
'is_default': False
}
if type_ is not None:
if type_ == 'OK':
default_kwargs['response'] = Gtk.ResponseType.OK
default_kwargs['text'] = 'OK'
elif type_ == 'Cancel':
default_kwargs['response'] = Gtk.ResponseType.CANCEL
default_kwargs['text'] = _('Cancel')
else:
raise ValueError('Unknown button type: %s ' % type_)
default_kwargs.update(kwargs)
return cls(**default_kwargs)
class HigDialog(Gtk.MessageDialog): class HigDialog(Gtk.MessageDialog):
def __init__(self, parent, type_, buttons, pritext, sectext, def __init__(self, parent, type_, buttons, pritext, sectext,
on_response_ok=None, on_response_cancel=None, on_response_yes=None, on_response_ok=None, on_response_cancel=None, on_response_yes=None,
@ -952,18 +984,22 @@ class ChangePasswordDialog(Gtk.Dialog):
class NewConfirmationDialog(Gtk.MessageDialog): class NewConfirmationDialog(Gtk.MessageDialog):
def __init__(self, text, sec_text, buttons, transient_for=None): def __init__(self, title, text, sec_text, buttons, transient_for=None):
Gtk.MessageDialog.__init__(self, Gtk.MessageDialog.__init__(self,
title=title,
text=text,
transient_for=transient_for, transient_for=transient_for,
message_type=Gtk.MessageType.QUESTION, message_type=Gtk.MessageType.QUESTION)
text=text)
self._buttons = buttons self._buttons = {}
for response, button in buttons.items(): for button in buttons:
self.add_button(button.text, response) self._buttons[button.response] = button
self.add_button(button.text, button.response)
if button.is_default:
self.set_default_response(button.response)
if button.action is not None: if button.action is not None:
widget = self.get_widget_for_response(response) widget = self.get_widget_for_response(button.response)
widget.get_style_context().add_class(button.action.value) widget.get_style_context().add_class(button.action.value)
self.format_secondary_markup(sec_text) self.format_secondary_markup(sec_text)
@ -972,7 +1008,7 @@ class NewConfirmationDialog(Gtk.MessageDialog):
self.run() self.run()
def _on_response(self, dialog, response): def _on_response(self, _dialog, response):
if response == Gtk.ResponseType.DELETE_EVENT: if response == Gtk.ResponseType.DELETE_EVENT:
# Look if DELETE_EVENT is mapped to another response # Look if DELETE_EVENT is mapped to another response
response = self._buttons.get(response, None) response = self._buttons.get(response, None)
@ -986,7 +1022,7 @@ class NewConfirmationDialog(Gtk.MessageDialog):
return return
if button.callback is not None: if button.callback is not None:
button.callback() button.callback(*button.args, **button.kwargs)
self.destroy() self.destroy()

View File

@ -24,10 +24,10 @@ from gajim.common import app
from gajim.common.nec import NetworkEvent from gajim.common.nec import NetworkEvent
from gajim.common.i18n import _ from gajim.common.i18n import _
from gajim.common.const import StyleAttr from gajim.common.const import StyleAttr
from gajim.common.const import DialogButton
from gajim.common.const import ButtonAction from gajim.common.const import ButtonAction
from gajim.gtk.dialogs import ErrorDialog from gajim.gtk.dialogs import ErrorDialog
from gajim.gtk.dialogs import DialogButton
from gajim.gtk.dialogs import NewConfirmationDialog from gajim.gtk.dialogs import NewConfirmationDialog
from gajim.gtk.util import get_builder from gajim.gtk.util import get_builder
@ -320,16 +320,16 @@ class Themes(Gtk.ApplicationWindow):
self._add_option_button.set_sensitive(False) self._add_option_button.set_sensitive(False)
self._clear_options() self._clear_options()
buttons = { NewConfirmationDialog(
Gtk.ResponseType.CANCEL: DialogButton('Keep Theme'), _('Delete'),
Gtk.ResponseType.OK: DialogButton('Delete', _('Delete Theme'),
_remove_theme, _('Do you want to permanently '
ButtonAction.DESTRUCTIVE), 'delete this theme?'),
} [DialogButton.make('Cancel'),
DialogButton.make('OK',
NewConfirmationDialog('Delete Theme', text=_('Delete'),
'Do you want to permanently delete this theme?', callback=_remove_theme,
buttons, action=ButtonAction.DESTRUCTIVE)],
transient_for=self) transient_for=self)
@staticmethod @staticmethod