Skip to content

Commit

Permalink
Fixed: format button activated after invalid changes (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdSpirit0 authored May 30, 2024
1 parent bb39070 commit e177c2f
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions lib/mintstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def devices_changed_callback(client):
self.filesystemlist.connect("changed", self.filesystem_selected)
self.devicelist.connect("changed", self.device_selected)

self.label_entry.connect("changed", lambda *_: self.update_format_button())
self.filesystemlist.connect("changed", lambda *_: self.update_format_button())
self.devicelist.connect("changed", lambda *_: self.update_format_button())

# Filesystemlist
self.fsmodel = Gtk.ListStore(str, str, int, bool, bool)
# id label max-length force-upper-case force-alpha-numeric
Expand Down Expand Up @@ -161,7 +165,6 @@ def devices_changed_callback(client):
self.filesystemlist.set_active_iter(itererator)
itererator = self.fsmodel.iter_next(itererator)

self.filesystem_selected(self.filesystemlist)
self.get_devices()

if usb_path_arg is not None:
Expand Down Expand Up @@ -252,8 +255,8 @@ def filesystem_selected(self, widget):
self.filesystem = self.fsmodel.get_value(itererator, 0)
self.activate_devicelist()

self.fix_label_entry_text(True)
self.label_entry.set_max_length(self.fsmodel.get_value(itererator, 2))
self.on_label_entry_text_changed(self, self.label_entry)

def file_selected(self, widget):
self.activate_devicelist()
Expand All @@ -263,13 +266,11 @@ def file_selected(self, widget):
else:
self.verify_button.set_sensitive(False)

def on_label_entry_text_changed(self, widget, data=None):
self.label_entry.handler_block(self.label_entry_changed_id)
def fix_label_entry_text(self, should_block_handler):
if should_block_handler:
self.label_entry.handler_block(self.label_entry_changed_id)

active_iter = self.filesystemlist.get_active_iter()
value = self.fsmodel.get_value(active_iter, 0)
cursor_pos = self.label_entry.props.cursor_position

text = self.label_entry.get_text()

if self.fsmodel.get_value(active_iter, 3):
Expand All @@ -278,6 +279,7 @@ def on_label_entry_text_changed(self, widget, data=None):
if self.fsmodel.get_value(active_iter, 4):
for char in FORBIDDEN_CHARS:
text = text.replace(char, "")

try:
text = unidecode(text)
except Exception as e:
Expand All @@ -286,13 +288,23 @@ def on_label_entry_text_changed(self, widget, data=None):
text = "USB STICK"

self.label_entry.set_text(text)
self.go_button.set_sensitive(len(text) > 0)

length = self.label_entry.get_buffer().get_length()
self.label_entry.select_region(length, -1)
self.label_entry.set_position(cursor_pos)
if should_block_handler:
self.label_entry.handler_unblock(self.label_entry_changed_id)

def on_label_entry_text_changed(self, widget, data=None):
self.fix_label_entry_text(True)

def update_format_button(self):
has_valid_device = self.devicelist.get_active_iter() is not None
has_valid_fs = self.filesystemlist.get_active_iter() is not None
is_enough_length = self.label_entry.get_buffer().get_length() > 0

if has_valid_device and has_valid_fs and is_enough_length:
self.go_button.set_sensitive(True)
else:
self.go_button.set_sensitive(False)

self.label_entry.handler_unblock(self.label_entry_changed_id)

def do_format(self, widget):
if self.debug:
Expand Down

0 comments on commit e177c2f

Please sign in to comment.