Module: Lich::Common::GUI::WindowSettings

Defined in:
documented/common/gui/window_settings.rb

Constant Summary collapse

SETTINGS_FILE =
'login_gui_settings.yml'
MIN_DIMENSION =
100
DARWIN_SPACER =
28

Class Method Summary collapse

Class Method Details

.apply_to_window(window, settings) ⇒ void

This method returns an undefined value.

Applies the given settings to the specified window.

Parameters:

  • window (Object)

    the window to apply settings to

  • settings (Hash)

    the settings to apply, including width, height, and position



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'documented/common/gui/window_settings.rb', line 57

def apply_to_window(window, settings)
  return if settings.empty?

  width = [settings[:width], MIN_DIMENSION].max
  height = [settings[:height], MIN_DIMENSION].max
  position = settings[:position]

  window.resize(width, height)

  return unless valid_position?(position)

  constrained_position = constrain_to_monitor(position, width, height)
  spacer = darwin? ? DARWIN_SPACER : 0
  window.move(constrained_position[0], constrained_position[1] + spacer)
end

.capture_geometry(window) ⇒ Hash

Captures the current geometry of the specified window.

Parameters:

  • window (Object)

    the window to capture geometry from

Returns:

  • (Hash)

    a hash containing the width, height, and position of the window



76
77
78
79
80
81
82
# File 'documented/common/gui/window_settings.rb', line 76

def capture_geometry(window)
  {
    width: window.allocation.width,
    height: window.allocation.height,
    position: window.position
  }
end

.load(data_dir) ⇒ Hash

Loads window settings from a YAML file.

Parameters:

  • data_dir (String)

    the directory where the settings file is located

Returns:

  • (Hash)

    the loaded settings or an empty hash if loading fails

Raises:

  • (StandardError)

    if there is an error reading the file



18
19
20
21
22
23
24
25
26
27
# File 'documented/common/gui/window_settings.rb', line 18

def load(data_dir)
  settings_file = File.join(data_dir, SETTINGS_FILE)
  return {} unless File.exist?(settings_file)

  settings = YAML.safe_load(File.read(settings_file), permitted_classes: [Symbol], symbolize_names: true)
  validate_settings(settings) ? settings : {}
rescue StandardError => e
  Lich.log "warning: Could not load window settings: #{e.message}"
  {}
end

.save(data_dir, width:, height:, position:) ⇒ Boolean

Saves the window settings to a YAML file.

Parameters:

  • data_dir (String)

    the directory where the settings file will be saved

  • width (Integer)

    the width of the window

  • height (Integer)

    the height of the window

  • position (Array<Integer>)

    the position of the window as [x, y]

Returns:

  • (Boolean)

    true if the settings were saved successfully, false otherwise

Raises:

  • (StandardError)

    if there is an error writing to the file



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'documented/common/gui/window_settings.rb', line 36

def save(data_dir, width:, height:, position:)
  return false unless valid_dimensions?(width, height) && valid_position?(position)

  settings_file = File.join(data_dir, SETTINGS_FILE)
  settings = {
    width: width,
    height: height,
    position: position
  }

  File.open(settings_file, 'w') { |f| f.write(YAML.dump(settings)) }
  true
rescue StandardError => e
  Lich.log "warning: Could not save window settings: #{e.message}"
  false
end