Skip to content

Commit

Permalink
fix(Disks Menu): Refactor disks menu to work with the new focus methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
pastaq authored and ShadowApex committed Aug 25, 2024
1 parent 3ed840f commit 82f57ca
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 50 deletions.
3 changes: 3 additions & 0 deletions core/systems/threading/shared_thread.gd
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func _init(options: int = Option.WATCHDOG_ENABLE as int) -> void:

func _notification(what: int):
if what == NOTIFICATION_PREDELETE:
if not self:
logger.error("Unable to stop thread, lost reference to self... Somehow...")
return
stop()


Expand Down
2 changes: 2 additions & 0 deletions core/ui/card_ui/card_ui.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ size_flags_vertical = 0
unique_name_in_owner = true
visible = false
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 4

[node name="OnScreenKeyboard" parent="AlwaysVisibleContent" instance=ExtResource("18_462u5")]
visible = false
Expand Down
8 changes: 7 additions & 1 deletion core/ui/card_ui/settings/disks_settings_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const drive_card_scene: PackedScene = preload("res://core/ui/components/drive_ca

var logger := Log.get_logger("DisksMenu", Log.LEVEL.INFO)

@onready var container: HFlowContainer = $%DriveCardContainer
@onready var container: VBoxContainer = $%DriveCardContainer
@onready var focus_group: FocusGroup = $%FocusGroup
@onready var no_drive_label: Label = $%NoDisksLabel

Expand Down Expand Up @@ -42,6 +42,12 @@ func _on_drives_updated(devices: Array[BlockDevice]) -> void:
# Poplulate drives
var last_focus: FocusGroup
for drive in devices:
var drive_type = drive.dbus_path.trim_prefix(steam_disks.BLOCK_PREFIX)

# Ignore loop devices
if drive_type.contains("loop"):
continue

# Create Drive Card
var drive_card := drive_card_scene.instantiate()
container.add_child(drive_card)
Expand Down
2 changes: 1 addition & 1 deletion core/ui/card_ui/settings/disks_settings_menu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Verify your media device is plugged in and that it is not mounted to a protected
horizontal_alignment = 1
uppercase = true

[node name="DriveCardContainer" type="HFlowContainer" parent="MarginContainer"]
[node name="DriveCardContainer" type="VBoxContainer" parent="MarginContainer"]
unique_name_in_owner = true
visible = false
layout_mode = 2
Expand Down
13 changes: 8 additions & 5 deletions core/ui/components/dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ signal choice_selected(accepted: bool)
@export var close_on_selected := true

@onready var label := $%Label as Label
@onready var confirm_button := $%ConfirmButton as Button
@onready var cancel_button := $%CancelButton as Button
@onready var confirm_button := $%ConfirmButton as CardButton
@onready var cancel_button := $%CancelButton as CardButton
@onready var fade_effect := $%FadeEffect as Effect


var _return_node: Control = null
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
confirm_button.button_up.connect(_on_selected.bind(true))
Expand All @@ -53,17 +53,20 @@ func _on_selected(accepted: bool) -> void:
if close_on_selected:
closed.emit()
choice_selected.emit(accepted)
if _return_node:
_return_node.grab_focus.call_deferred()
_return_node = null


## Opens the dialog box with the given settings
func open(message: String = "", confirm_txt: String = "", cancel_txt: String = "") -> void:
func open(return_node: Control, message: String = "", confirm_txt: String = "", cancel_txt: String = "") -> void:
if message != "":
text = message
if confirm_txt != "":
confirm_text = confirm_txt
if cancel_txt != "":
cancel_text = cancel_txt

_return_node = return_node
opened.emit()
await fade_effect.effect_finished
confirm_button.grab_focus.call_deferred()
23 changes: 4 additions & 19 deletions core/ui/components/dialog.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=5 format=3 uid="uid://eqqk1uve143x"]
[gd_scene load_steps=6 format=3 uid="uid://eqqk1uve143x"]

[ext_resource type="Script" path="res://core/ui/components/dialog.gd" id="1_yqaeg"]
[ext_resource type="PackedScene" uid="uid://bw8113ocotx2r" path="res://core/systems/effects/fade_effect.tscn" id="2_q0yos"]
[ext_resource type="PackedScene" uid="uid://ekhjpmat02f8" path="res://core/systems/effects/slide_effect.tscn" id="3_hbrr5"]
[ext_resource type="PackedScene" uid="uid://8m20p2s0v5gb" path="res://core/systems/input/focus_group.tscn" id="4_vp527"]
[ext_resource type="PackedScene" uid="uid://c71ayw7pcw6u6" path="res://core/ui/components/card_button.tscn" id="5_clb01"]

[node name="Dialog" type="Control" groups=["dialog", "popup"]]
editor_description = "Opens a dialog choice to the user."
Expand Down Expand Up @@ -80,26 +81,10 @@ alignment = 1

[node name="FocusGroup" parent="Spacer/CenterContainer/PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer" instance=ExtResource("4_vp527")]

[node name="CancelButton" type="Button" parent="Spacer/CenterContainer/PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer"]
[node name="CancelButton" parent="Spacer/CenterContainer/PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer" instance=ExtResource("5_clb01")]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
focus_neighbor_left = NodePath("../ConfirmButton")
focus_neighbor_top = NodePath(".")
focus_neighbor_right = NodePath("../ConfirmButton")
focus_neighbor_bottom = NodePath(".")
focus_next = NodePath("../ConfirmButton")
focus_previous = NodePath("../ConfirmButton")
text = "No"

[node name="ConfirmButton" type="Button" parent="Spacer/CenterContainer/PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer"]
[node name="ConfirmButton" parent="Spacer/CenterContainer/PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer" instance=ExtResource("5_clb01")]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
focus_neighbor_left = NodePath("../CancelButton")
focus_neighbor_top = NodePath(".")
focus_neighbor_right = NodePath("../CancelButton")
focus_neighbor_bottom = NodePath(".")
focus_next = NodePath("../CancelButton")
focus_previous = NodePath("../CancelButton")
text = "Yes"
84 changes: 72 additions & 12 deletions core/ui/components/drive_card.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ var device: BlockDevice
var device_path: String
var highlight_tween: Tween

signal pressed
signal button_up
signal button_down
signal nonchild_focused
signal format_drive(device: BlockDevice)
signal format_sd_card
signal init_partition(device: PartitionDevice)
Expand All @@ -24,8 +28,9 @@ signal init_partition(device: PartitionDevice)
@onready var drive_icon: TextureRect = $%IconTextureRect
@onready var drive_focus_group: FocusGroup = $%DriveFocusGroup
@onready var format_button: CardButton = $%FormatButton
@onready var partitions_container: VBoxContainer = $%PartitionsVBox
@onready var partitions_container: HBoxContainer = $%PartitionsHBox
@onready var partitions_focus_group: FocusGroup = $%PartitionsFocusGroup
@onready var highlight := $%HighlightTexture as TextureRect

var logger: Logger
var log_level:= Log.LEVEL.INFO
Expand All @@ -36,6 +41,7 @@ func setup(device: BlockDevice) -> void:
# Setup UDisks2 information
self.device = device
self.device_path = "/dev" + self.device.dbus_path.trim_prefix(steam_disks.BLOCK_PREFIX)

logger = Log.get_logger("DriveCard|"+self.device_path, log_level)
logger.debug("Setup Drive Card")
drive_name_label.text = self.device_path
Expand All @@ -52,20 +58,30 @@ func setup(device: BlockDevice) -> void:
format_button.pressed.disconnect(_srm_format_drive)
format_button.visible = true

var on_focus_exited := func():
self._on_unfocus.call_deferred()
focus_exited.connect(on_focus_exited)
theme_changed.connect(_on_theme_changed)

# Find the parent theme and update if required
var effective_theme := ThemeUtils.get_effective_theme(self)
if effective_theme:
_on_theme_changed()


func _srm_format_drive() -> void:
if steam_disks.block_operations:
logger.debug("Format operation blocked.")
return

var dialog := get_tree().get_first_node_in_group("dialog") as Dialog
var msg := "WARNING: All data on " + device_path + " device will be wiped. " + \
"This action cannot be undone. Do you wish to continue?"
dialog.open(msg, "Cancel", "Continue Format")
dialog.open(self, msg, "Cancel", "Continue Format")
var cancel := await dialog.choice_selected as bool
if cancel:
return

if steam_disks.block_operations:
logger.debug("Format operation blocked.")
return

logger.debug("Format Drive", device_path)
_clear_partitions()
_on_format_started()
Expand All @@ -76,18 +92,18 @@ func _srm_format_drive() -> void:


func _srm_format_sd_card() -> void:
if steam_disks.block_operations:
logger.debug("Format operation blocked.")
return

var dialog := get_tree().get_first_node_in_group("dialog") as Dialog
var msg := "WARNING: All data on " + device_path + " device will be wiped. " + \
"This action cannot be undone. Do you wish to continue?"
dialog.open(msg, "Cancel", "Continue Format")
dialog.open(format_button, msg, "Cancel", "Continue Format")
var cancel := await dialog.choice_selected as bool
if cancel:
return

if steam_disks.block_operations:
logger.debug("Format operation blocked.")
return

logger.debug("Format SD Card", device_path)
_clear_partitions()
_on_format_started()
Expand Down Expand Up @@ -116,7 +132,6 @@ func _srm_init_drive(partition: PartitionDevice) -> void:
init_partition.emit(partition)



func _clear_partitions() -> void:
# Clear the current grid of items
var keep_nodes := [partitions_focus_group]
Expand Down Expand Up @@ -150,9 +165,54 @@ func _populate_partitions() -> void:
_clear_partitions()
var last_focus: FocusGroup
for partition in self.device.partitions:

# Ignore loop devices
if partition.partition_name.contains("/dev/loop"):
continue

logger.debug("Drive has partition to set up:", partition.dbus_path)
var partition_card := partition_card_scene.instantiate() as PartitionCard
partitions_container.add_child(partition_card)
partition_card.setup(partition)
partition_card.visible = true
partition_card.init_partition.connect(_srm_init_drive)


# Update the highlight texture on theme change
func _on_theme_changed() -> void:
# Configure the highlight texture from the theme
var highlight_texture := get_theme_icon("highlight", "ExpandableCard")
if highlight_texture:
highlight.texture = highlight_texture


func _gui_input(event: InputEvent) -> void:
if event.is_action("ui_accept"):
if event.is_pressed():
button_down.emit()
pressed.emit()
else:
button_up.emit()

func _on_unfocus() -> void:
# Emit a signal if a non-child node grabs focus
var focus_owner := get_viewport().gui_get_focus_owner()
if not self.is_ancestor_of(focus_owner):
nonchild_focused.emit()
return

# If a child has focus, listen for focus changes until a non-child has focus
get_viewport().gui_focus_changed.connect(_on_focus_change)


func _on_focus_change(focused: Control) -> void:
# Don't do anything if the focused node is a child
if self.is_ancestor_of(focused):
return

# If a non-child has focus, emit a signal to indicate that this node and none
# of its children have focus.
nonchild_focused.emit()
var viewport := get_viewport()
if viewport.gui_focus_changed.is_connected(_on_focus_change):
viewport.gui_focus_changed.disconnect(_on_focus_change)
Loading

0 comments on commit 82f57ca

Please sign in to comment.