From 1391f70d79144518dda9d0294a9e8a8fc79904ff Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 24 Dec 2015 19:37:37 -0800 Subject: preferences_window.py: Attempts to limit input character length Tried to create an `NSFormatter` subclass but kept getting this stupid error: TypeError: isPartialStringValid:newEditingString:errorDescription:: Need tuple of 3 arguments as result and: TypeError: isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription:: Need tuple of 4 arguments as result which is freaking idiotic because if you look at the Cocoa documentation you can see that both those methods return fraking `BOOL`s. Also needed to keep renaming the subclass because every time I reinstalled the extension, PyObjC or RoboFont or whatever would yell: Traceback (most recent call last): File "preferences_window.py", line 10, in error: Booya2HotKeyFormatter is overriding existing Objective-C class Anyway, ended up ditching the `NSFormatter` subclass because of that _stupid_ nonsensical error and am now just checking for string length in the input edit callback and forcing a max length there by resetting the value to ensure it doesn't go over 1 character. --- preferences_window.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/preferences_window.py b/preferences_window.py index c7b479b..4a9afa0 100644 --- a/preferences_window.py +++ b/preferences_window.py @@ -1,7 +1,47 @@ +from Foundation import NSFormatter + import vanilla from preferences import Preferences +# from remote_pdb import RemotePdb + + +# class Booya4HotKeyFormatter(NSFormatter): +# +# def __new__(cls, *arg, **kwargs): +# self = cls.alloc().init() +# return self +# +# def stringForObjectValue_(self, obj): +# return str(obj) +# +# def getObjectValue_forString_errorDescription_(self, obj, string, error): +# return True +# +# def isPartialStringValid_newEditingString_errorDescription_( +# self, +# partial_string, +# new_string, +# error): +# if len(partial_string) > 1: +# return False +# +# return True + + # def isPartialStringValid_proposedSelectedRange_originalString_originalSelectedRange_errorDescription_( + # self, + # partial_string_ptr, + # proposed_sel_range_ptr, + # orig_string, + # orig_sel_range, + # error): + # if len(partial_string_ptr) > 1: + # return False + # + # return True + + class PreferencesWindow(object): @@ -12,14 +52,22 @@ class PreferencesWindow(object): self.w.activation_key_label = vanilla.TextBox( (10, 15, -10, 22), 'Short Key:') + # RemotePdb('127.0.0.1', 4444).set_trace() self.w.activation_key = vanilla.EditText( posSize=(82, 12, -10, 25), text=self.preferences.activation_key, + # formatter=Booya4HotKeyFormatter(), callback=self.edit_text_callback) self.w.open() def edit_text_callback(self, sender): - self.preferences.activation_key = sender.get() + r = sender.get() + + if len(r) > 1: + r = r[0] + + self.preferences.activation_key = r + self.w.activation_key.set(r) PreferencesWindow() -- cgit v1.2.3