Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions hab_gui/windows/alias_launch_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ def __init__(
self.process_entry_points()
self.init_gui(uri)

# Window properties
self.setFixedWidth(400)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image image

The default minimum size is a bit small even if there are aliases to show. Should we keep the 400 but use self.setMinimumWidth(400) instead of self.setFixedWidth(400)?

self.center_window_position()

# Create a auto-refresh timer by default that forces a refresh of hab.
# This can be disabled by setting the site config setting to an empty string.
self.refresh_timer = QtCore.QTimer(self)
Expand Down Expand Up @@ -104,6 +100,11 @@ def apply_layout(self):
else:
self.setTabOrder(self.uri_widget, self.pinned_uris)

def closeEvent(self, event): # noqa: N802
"""Saves the prefs on close if prefs are enabled."""
self.record_prefs()
super().closeEvent(event)

def process_entry_points(self):
"""Loads the classes defined by the site entry_point system.
These are later initialized by init_gui to create the UI.
Expand Down Expand Up @@ -192,6 +193,12 @@ def init_gui(self, uri=None):
# Ensure the window title always shows the currently selected URI
self.settings.uri_changed.connect(self._update_window_title)

# Window properties
self.center_window_position()

# Restore prefs
self.restore_prefs()

@utils.cursor_override()
def refresh_cache(self, reset_timer=True):
"""Refresh the resolved hab and re-display.
Expand Down Expand Up @@ -222,6 +229,41 @@ def center_window_position(self):
qt_rectangle.moveCenter(center_point)
self.move(qt_rectangle.topLeft())

def restore_prefs(self):
"""Restore various saved prefs. It will only do that if prefs are
enabled. This will call load to ensure the preference file has been
loaded.
"""
prefs = self.settings.resolver.user_prefs()
if not prefs.enabled:
return
# Ensure the preferences are loaded.
prefs.load()

# Restore previous geometry
geom = prefs.get("geometry", None)
if geom:
self.setGeometry(*geom)

logger.debug(f"Prefs restored {prefs.filename}")

def record_prefs(self):
"""Save various prefs. It will only do that if prefs are enabled. This
will call load to ensure the preference file has been loaded.
"""
prefs = self.settings.resolver.user_prefs()
if not prefs.enabled:
return
# Ensure the preferences are loaded.
prefs.load()

# Save window geometry
prefs["geometry"] = self.geometry().getRect()

# Save the prefs
prefs.save()
logger.debug(f"Prefs saved {prefs.filename}")

Comment on lines +232 to +266
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The user prefs are used for more than just this widget or even hab-gui. The top level pref name geometry is too vague. Also I think we should assume that there will be other prefs required in the future, so we should put all of this widgets preferences under a container dictionary. Something like "alias_launcher": {"geometry": [641, 376, 286, 234]}. Then other plugins or future features can add to its dictionary.
  2. There is a simpler hab_gui interface for working with user_prefs that currently is only used in one place. We might as well make this use the new simpler interface that takes care of the boilerplate code you had to replicate.

This code addresses both of these notes.

    def restore_prefs(self):
        """Restore various saved prefs. It will only do that if prefs are
        enabled. This will call load to ensure the preference file has been
        loaded.
        """
        prefs = self.settings.user_pref("alias_launcher", {})

        # Restore previous geometry
        geom = prefs.get("geometry", None)
        if geom:
            self.setGeometry(*geom)

    def record_prefs(self):
        """Save various prefs. It will only do that if prefs are enabled. This
        will call load to ensure the preference file has been loaded.
        """
        # Use the namespace alias_launcher to store this and any future UI prefs.
        prefs = self.settings.user_pref("alias_launcher", {})

        # Save window geometry
        prefs["geometry"] = self.geometry().getRect()

        self.settings.set_user_pref("alias_launcher", prefs)

This code no longer needs to get the prefs so I removed the prefs.filename logging messages as I would have to resolve the prefs object just to log it. If we decide we actually want that verbosity of logging we should probably put it into Settings, or the hab package itself.


def main():
app = QtWidgets.QApplication([])
Expand Down