Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Move from has-focus to contains-focus for focused
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Mar 25, 2024
1 parent 2cb3c95 commit a4ffbd4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Sources/Adwaita/View/Modifiers/InspectorWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ struct InspectorWrapper: Widget {

extension View {

/// The identifier for the focus event controller.
static var focus: String { "focus" }

/// Modify a GTUI widget before being displayed and when being updated.
/// - Parameter modify: Modify the widget.
/// - Returns: A view.
Expand Down Expand Up @@ -142,14 +145,22 @@ extension View {
/// - Parameter focus: Whether the view is focused.
/// - Returns: A view.
public func focused(_ focused: Binding<Bool>) -> View {
inspect { storage in
storage.notify(name: "has-focus", id: "focused") {
let newValue = gtk_widget_has_focus(storage.pointer?.cast()) != 0
inspectOnAppear { storage in
let controller = gtk_event_controller_focus_new()
storage.content[Self.focus] = [.init(controller)]
gtk_widget_add_controller(storage.pointer?.cast(), controller)
}
.inspect { storage in
guard let controller = storage.content[Self.focus]?.first else {
return
}
controller.notify(name: "contains-focus", id: "focused") {
let newValue = gtk_event_controller_focus_contains_focus(controller.pointer) != 0
if focused.wrappedValue != newValue {
focused.wrappedValue = newValue
}
}
if focused.wrappedValue != (gtk_widget_has_focus(storage.pointer?.cast()) != 0) {
if focused.wrappedValue != (gtk_event_controller_focus_contains_focus(controller.pointer) != 0) {
gtk_widget_grab_focus(storage.pointer?.cast())
}
}
Expand Down

0 comments on commit a4ffbd4

Please sign in to comment.