Make breakpoints check condition when initializing
All checks were successful
Deploy Docs / publish (push) Successful in 21m20s
SwiftLint / SwiftLint (push) Successful in 5s

This commit is contained in:
david-swift 2024-10-28 21:38:00 +01:00
parent e2da50703c
commit d93bc321a2
2 changed files with 33 additions and 13 deletions

View File

@ -27,7 +27,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://git.aparoksha.dev/aparoksha/meta", from: "0.1.0"),
.package(url: "https://git.aparoksha.dev/aparoksha/meta", branch: "main"),
.package(url: "https://git.aparoksha.dev/aparoksha/meta-sqlite", from: "0.1.0"),
.package(
url: "https://git.aparoksha.dev/aparoksha/levenshtein-transformations",

View File

@ -30,34 +30,34 @@ public struct BreakpointBin: AdwaitaWidget {
/// The condition.
@Property(
set: { bin, condition, storage in
let string: OpaquePointer
var condition = condition
switch condition {
case let .naturalWidth(padding):
let child = adw_breakpoint_bin_get_child(bin.cast())
var size: Int32 = 0
gtk_widget_measure(child, GTK_ORIENTATION_HORIZONTAL, -1, nil, &size, nil, nil)
string = adw_breakpoint_condition_parse(BreakpointCondition.minWidth(.init(size) + padding).condition)
condition = .minWidth(.init(size) + padding)
case let .naturalHeight(padding):
let child = adw_breakpoint_bin_get_child(bin.cast())
var size: Int32 = 0
gtk_widget_measure(child, GTK_ORIENTATION_VERTICAL, -1, nil, &size, nil, nil)
string = adw_breakpoint_condition_parse(BreakpointCondition.minHeight(.init(size) + padding).condition)
condition = .minHeight(.init(size) + padding)
default:
string = adw_breakpoint_condition_parse(condition.condition)
break
}
storage.fields["condition"] = condition
let string = adw_breakpoint_condition_parse(condition.condition)
if let breakpoint = storage.fields["breakpoint"] as? OpaquePointer {
g_object_unref(adw_breakpoint_get_condition(breakpoint)?.cast())
adw_breakpoint_set_condition(breakpoint, string)
} else {
let breakpoint = adw_breakpoint_new(string); adw_breakpoint_bin_add_breakpoint(bin.cast(), breakpoint)
let breakpoint = adw_breakpoint_new(string)
adw_breakpoint_bin_add_breakpoint(bin.cast(), breakpoint)
storage.fields["breakpoint"] = breakpoint
if let bin = storage.content["_content"]?.first?.content["append"]?.first {
adw_breakpoint_add_setter(
breakpoint,
bin.opaquePointer?.cast(),
"visible",
false.gValue
)
if let matches = storage.fields["binding"] as? Binding<Bool> {
Idle {
condition.initialize(child: adw_breakpoint_bin_get_child(bin.cast()), matches: matches)
}
}
}
},
@ -71,6 +71,7 @@ public struct BreakpointBin: AdwaitaWidget {
storage.notify(name: "current-breakpoint") {
matches.wrappedValue = adw_breakpoint_bin_get_current_breakpoint(bin.cast()) != nil
}
storage.fields["binding"] = matches
},
set: { _, _, _ in },
pointer: OpaquePointer.self
@ -131,4 +132,23 @@ public enum BreakpointCondition: Equatable {
}
}
/// Initialize the breakpoint when initializing the view.
/// - Parameters:
/// - child: The widget.
/// - matches: The matches binding.
func initialize(child: UnsafeMutablePointer<GtkWidget>?, matches: Binding<Bool>) {
switch self {
case let .maxWidth(width):
matches.wrappedValue = gtk_widget_get_width(child) <= width
case let .minWidth(width):
matches.wrappedValue = gtk_widget_get_width(child) >= width
case let .maxHeight(height):
matches.wrappedValue = gtk_widget_get_height(child) <= height
case let .minHeight(height):
matches.wrappedValue = gtk_widget_get_height(child) >= height
default:
break
}
}
}