Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.0.7 (Rework keyboard) #11

Merged
merged 23 commits into from
Oct 15, 2024
Merged

v1.0.7 (Rework keyboard) #11

merged 23 commits into from
Oct 15, 2024

Conversation

pilgrimtabby
Copy link
Owner

@pilgrimtabby pilgrimtabby commented Aug 6, 2024

This PR shifts the code handling keyboard listener/controller backends from ui_controller to a new module called ui_keyboard_controller. This is done to keep keyboard handling in ui_controller abstract, and makes it easier to handle the complexities of using two different backends depending on the platform.

In addition, this PR adds some unit tests for settings.py, splitter.py, and pilgrim_autosplitter.py. This whole project should have unit tests but I have limited time to devote to this right now, so it's not a priority.

It also fixes several bugs, most related to hotkey implementation:

  • Hotkeys will no longer be bound to multiple keys when their key codes match (pynput only)
  • Hotkeys can no longer be bound if they have a key code but no name (pynput) -- this prevents the user from thinking no key is bound when in reality there is one
  • ui._poll is now called at the correct interval even after a settings change (whoops)
  • Fix implementation issue that could cause segmentation fault occasionally when binding hotkeys

Other misc. changes/updates:

  • Automatically resize the font if hotkey names are larger than the hotkey box
  • On MacOS, make calls to caffeinate in separate thread to avoid bogging down main thread
  • Typehints are now uniform in style and backwards compatible with Python 3.8+
  • Some class attributes and methods have been renamed for brevity and readability
  • Pytest added to requirements.txt and requirements-linux.txt
  • pilgrim_autosplitter.py rewritten for clarity and neatness
  • settings.py refactored to allow using different QSettings in functions for testing purposes
  • Some methods in splitter.py refactored and/or split into multiple methods for testing purposes or for clarity

ui_keyboard_controller lacks some typing information because the relevant libraries won't be imported on all platforms.

Changes:
-Handle key presses, releases, listener setup, etc. from a new module, ui_keyboard_controller, so that ui_controller doesn't become cluttered with platform-dependent variables and library calls. The new module is a wrapper that keeps things abstract and simple in the controller.
-Rename _handle_hotkey_press and _execute_split_action to _react_to_hotkey_flags and _react_to_split_flags, respectively, to avoid confusion.
Changes:
-Don't handle keys in _handle_key_press if they have a key code but no name in pynput -- this can lead to hotkeys that are assigned but look blank to the user
-Before setting a hotkey to "pressed" in _handle_key_press, make sure the key name AND code match. Sometimes key codes can match even when names don't, leading to secret hotkeys being unexpectedly created (I'm looking at you, MacOS)
-Automatically resize hotkey names in the settings menu if they are wider than the text box. To accomplish this, add a couple helper methods to KeyLineEdit
--Rename PREVIOUS_HOTKEY_NAME and PREVIOUS_HOTKEY_CODE to PREV_HOTKEY_NAME and PREV_HOTKEY_CODE, respectively, for brevity
FPS settings appeared to have no logical effect on the rate that _poll
was called in ui_controller. Turns out there was a logic error forcing
_poll to repeat at the interval from the most recent settings change,
not the actual setting. That is now fixed.
Make caffeinate calls in their own dedicated thread on MacOS since the
calls take up a decent amount of time, and the overhead needed to generate
a single thread is very small.
Previously, if the user was typing very quickly with a hotkey box in
focus in the settings menu, a segmentation fault could rarely occur.
This has been fixed: _handle_hotkey_press now sets flags instead of
modifying the GUI directly (something it has done for a long time to
press hotkeys, but didn't seem necessary for simply updating hotkey
values). A lock is used to avoid racing when updating the box.
-Remove test_init as it was useless
-Finish test_run to ensure threads are joined successfully
-Rename self.gui to self.app in pilgrim_autosplitter.py
-Join ui_keyboard threads when app closes
-Use pytest instead of unittest for unit tests
Switch to using the QApplication.aboutToQuit signal to call splitter
.safe_exit_all_threads so it is called more consistently before the
app exits. Also remove the run function to simplify things.
This is mostly for testing purposes, to make it easier to test
settings changes using dummy settings.
This commit also cleans up the code around aspect ratio and width/
height adjustments in set_defaults to remove some redundancy.
-Make set_next_capture_index return found_valid_source to make it
easy to check whether a new source was found or not

-Use _compare_thread.is_alive instead of (match_percent is None) to
decide what to do in toggle_suspended. It does the same thing and is
much easier to understand (and is less brittle)

-Break getting max fps into its own method, _get_max_fps

-Break waiting on the interval in _compare and _capture into its own
method, wait_for_interval
For now, it's not needed, since there is no advantage to stopping the
listener from pilgrim_autosplitter.py -- the listener never causes
segfaults and is killed automatically on exit because it's a daemon.
Maybe someday I'll want this, but for now it's pointless.
This PR adds some unit tests for settings.py, splitter.py, and pilgrim_autosplitter.py. This whole project should have unit tests but I have limited time to devote to this right now, so it's not a priority.

Changes:
-pytest added to requirements.txt and requirements-linux.txt
-pilgrim_autosplitter.py rewritten for clarity and neatness
-settings.py refactored to allow using different QSettings in functions for testing purposes
-some methods in splitter.py refactored and/or split into multiple methods for testing purposes
- Rename settings.set_defaults to settings.set_program_vals for clarity
- In settings.set_program_vals, check if last-used version was earlier than v1.0.7 and unset hotkeys if so. This is to prevent a bug related to the change in hotkey implementation in v1.0.7.
- Edits to settings.py for clarity and brevity
- Add unit test for settings.version_ge
- Change checking for updates in ui_main_window.py so it checks if there is a new version that is greater than the current version available (makes life easier when developing a new version, and is cleaner)
@pilgrimtabby pilgrimtabby merged commit 5ad3088 into main Oct 15, 2024
@pilgrimtabby pilgrimtabby deleted the rework-keyboard branch October 15, 2024 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant