Skip to content

Commit 9201a4f

Browse files
committed
form: Clean up pattern matching
1 parent a71f0c8 commit 9201a4f

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

openemail/widgets/form.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,18 @@ class FormField(GObject.Object):
3333
text = GObject.Property(type=str)
3434

3535
@GObject.Property(type=Gtk.Widget)
36-
def field(self) -> Gtk.Widget:
36+
def field(self) -> Gtk.Editable | Gtk.TextView:
3737
"""Get the field containing the text."""
3838
return self._field
3939

4040
@field.setter
41-
def field(self, field: Gtk.Widget) -> None:
42-
if isinstance(field, Gtk.Editable):
43-
field.bind_property(
44-
"text", self, "text", GObject.BindingFlags.BIDIRECTIONAL
45-
)
46-
field.connect("notify::text", lambda *_: self.validate())
47-
elif isinstance(field, Gtk.TextView):
48-
field.props.buffer.bind_property(
49-
"text", self, "text", GObject.BindingFlags.BIDIRECTIONAL
50-
)
51-
field.props.buffer.connect("notify::text", lambda *_: self.validate())
52-
else:
53-
msg = "FormField.field must be Gtk.Editable or Gtk.TextView"
54-
raise TypeError(msg)
55-
41+
def field(self, field: Gtk.Editable | Gtk.TextView) -> None:
5642
self._field = field
5743

44+
buffer = field.props.buffer if isinstance(field, Gtk.TextView) else field
45+
buffer.bind_property("text", self, "text", GObject.BindingFlags.BIDIRECTIONAL)
46+
buffer.connect("notify::text", lambda *_: self.validate())
47+
5848
def validate(self) -> None:
5949
"""Validate the form field."""
6050
match cast("FormFieldType", self.type):

0 commit comments

Comments
 (0)