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

Commit

Permalink
Replace modifiers by new widget data
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Aug 15, 2024
1 parent 9f83b23 commit fa34452
Show file tree
Hide file tree
Showing 69 changed files with 621 additions and 824 deletions.
33 changes: 16 additions & 17 deletions Sources/Adwaita/Menu/MenuButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,23 @@ public struct MenuButton: MenuWidget {
/// - type: The type of the views.
/// - Returns: The view storage.
public func container<Data>(
modifiers: [(any AnyView) -> any AnyView],
data: WidgetData,
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storage = ViewStorage(nil)
let getItem: (AdwaitaApp, AdwaitaWindow?) -> OpaquePointer? = { app, window in
var label = filteredLabel
storage.fields["app"] = app
if let window, preferApplicationWindow {
app.addKeyboardShortcut(shortcut, id: filteredLabel, window: window, handler: handler)
label = "win." + label
storage.fields["window"] = window
} else {
app.addKeyboardShortcut(shortcut, id: filteredLabel, handler: handler)
label = "app." + label
}
return g_menu_item_new(self.label, label)
var label = filteredLabel
guard let app = data.appStorage as? AdwaitaApp else {
return .init(nil)
}
storage.pointer = getItem
if let window = data.sceneStorage.pointer as? AdwaitaWindow, preferApplicationWindow {
app.addKeyboardShortcut(shortcut, id: filteredLabel, window: window, handler: handler)
label = "win." + label
} else {
app.addKeyboardShortcut(shortcut, id: filteredLabel, handler: handler)
label = "app." + label
}
let pointer = g_menu_item_new(self.label, label)
storage.pointer = pointer
return storage
}

Expand All @@ -68,14 +67,14 @@ public struct MenuButton: MenuWidget {
/// - type: The type of the views.
public func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
guard let app = storage.fields["app"] as? AdwaitaApp else {
guard let app = data.appStorage as? AdwaitaApp else {
return
}
if let window = storage.fields["window"] as? AdwaitaWindow, preferApplicationWindow {
if let window = data.sceneStorage.pointer as? AdwaitaWindow, preferApplicationWindow {
app.addKeyboardShortcut(shortcut, id: filteredLabel, window: window, handler: handler)
} else {
app.addKeyboardShortcut(shortcut, id: filteredLabel, handler: handler)
Expand Down
29 changes: 14 additions & 15 deletions Sources/Adwaita/Menu/MenuCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public struct MenuCollection: MenuWidget, Wrapper {
/// - type: The type of the views.
/// - Returns: The view storage.
public func container<Data>(
modifiers: [(any AnyView) -> any AnyView],
data: WidgetData,
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storages = content.storages(modifiers: modifiers, type: type)
let storages = content.storages(data: data, type: type)
return .init(nil, content: [.mainContent: storages])
}

Expand All @@ -41,25 +41,25 @@ public struct MenuCollection: MenuWidget, Wrapper {
/// - type: The type of the views.
public func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
guard let storages = storage.content[.mainContent] else {
return
}
content.update(storages, modifiers: modifiers, updateProperties: updateProperties, type: type)
content.update(storages, data: data, updateProperties: updateProperties, type: type)
}

/// Render the collection as a menu.
/// - Parameters:
/// - app: The app object.
/// - window: The window object.
/// - Parameter data: The widget data.
/// - Returns: The view storage with the GMenu as the pointer.
public func getMenu(app: AdwaitaApp, window: AdwaitaWindow?) -> ViewStorage {
public func getMenu(data: WidgetData) -> ViewStorage {
let menu = g_menu_new()
let storage = container(modifiers: [], type: MenuContext.self)
initializeMenu(menu: menu, storage: storage, app: app, window: window)
let storage = container(data: data.noModifiers, type: MenuContext.self)
if let app = data.appStorage as? AdwaitaApp, let window = data.sceneStorage.pointer as? AdwaitaWindow {
initializeMenu(menu: menu, storage: storage, app: app, window: window)
}
storage.pointer = menu
return storage
}
Expand All @@ -71,14 +71,13 @@ public struct MenuCollection: MenuWidget, Wrapper {
/// - app: The app object.
/// - window: The window object.
func initializeMenu(menu: OpaquePointer?, storage: ViewStorage, app: AdwaitaApp, window: AdwaitaWindow?) {
if storage.pointer == nil {
if let item = storage.opaquePointer {
g_menu_append_item(menu, item)
storage.pointer = item
} else {
for element in storage.content[.mainContent] ?? [] {
initializeMenu(menu: menu, storage: element, app: app, window: window)
}
} else if let item = (storage.pointer as? (AdwaitaApp, AdwaitaWindow?) -> OpaquePointer?) {
let item = item(app, window)
g_menu_append_item(menu, item)
storage.pointer = item
}
}

Expand Down
16 changes: 7 additions & 9 deletions Sources/Adwaita/Menu/MenuSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ public struct MenuSection: MenuWidget {
/// - type: The type of the views.
/// - Returns: The view storage.
public func container<Data>(
modifiers: [(any AnyView) -> any AnyView],
data: WidgetData,
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storage = ViewStorage(nil)
let getItem: (AdwaitaApp, AdwaitaWindow?) -> OpaquePointer? = { app, window in
let childStorage = MenuCollection { sectionContent }.getMenu(app: app, window: window)
storage.content[.mainContent] = [childStorage]
return g_menu_item_new_section(nil, childStorage.opaquePointer?.cast())
}
storage.pointer = getItem
let childStorage = MenuCollection { sectionContent }.getMenu(data: data)
storage.content[.mainContent] = [childStorage]
let pointer = g_menu_item_new_section(nil, childStorage.opaquePointer?.cast())
storage.pointer = pointer
return storage
}

Expand All @@ -46,15 +44,15 @@ public struct MenuSection: MenuWidget {
/// - type: The type of the views.
public func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
guard let content = storage.content[.mainContent]?.first else {
return
}
MenuCollection { sectionContent }
.updateStorage(content, modifiers: [], updateProperties: updateProperties, type: MenuContext.self)
.updateStorage(content, data: data, updateProperties: updateProperties, type: MenuContext.self)
}

}
16 changes: 7 additions & 9 deletions Sources/Adwaita/Menu/Submenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ public struct Submenu: MenuWidget {
/// - type: The type of the views.
/// - Returns: The view storage.
public func container<Data>(
modifiers: [(any AnyView) -> any AnyView],
data: WidgetData,
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storage = ViewStorage(nil)
let getItem: (AdwaitaApp, AdwaitaWindow?) -> OpaquePointer? = { app, window in
let childStorage = MenuCollection { content }.getMenu(app: app, window: window)
storage.content[.mainContent] = [childStorage]
return g_menu_item_new_submenu(label, childStorage.opaquePointer?.cast())
}
storage.pointer = getItem
let childStorage = MenuCollection { content }.getMenu(data: data)
storage.content[.mainContent] = [childStorage]
let pointer = g_menu_item_new_submenu(label, childStorage.opaquePointer?.cast())
storage.pointer = pointer
return storage
}

Expand All @@ -51,15 +49,15 @@ public struct Submenu: MenuWidget {
/// - type: The type of the views.
public func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
guard let content = storage.content[.mainContent]?.first else {
return
}
MenuCollection { self.content }
.updateStorage(content, modifiers: [], updateProperties: updateProperties, type: MenuContext.self)
.updateStorage(content, data: data, updateProperties: updateProperties, type: MenuContext.self)
}

}
10 changes: 5 additions & 5 deletions Sources/Adwaita/View/Dialogs/AboutDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ struct AboutDialog: AdwaitaWidget {
/// - modifiers: Modify views before being updated.
/// - type: The type of the app storage.
/// - Returns: The view storage.
func container<Data>(modifiers: [(AnyView) -> AnyView], type: Data.Type) -> ViewStorage where Data: ViewRenderData {
let storage = child.storage(modifiers: modifiers, type: type)
update(storage, modifiers: modifiers, updateProperties: true, type: type)
func container<Data>(data: WidgetData, type: Data.Type) -> ViewStorage where Data: ViewRenderData {
let storage = child.storage(data: data, type: type)
update(storage, data: data, updateProperties: true, type: type)
return storage
}

Expand All @@ -51,11 +51,11 @@ struct AboutDialog: AdwaitaWidget {
/// - type: The type of the app storage.
func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
child.updateStorage(storage, modifiers: modifiers, updateProperties: updateProperties, type: type)
child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
guard updateProperties, (storage.previousState as? Self)?.visible != visible else {
return
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Adwaita/View/Dialogs/AlertDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public struct AlertDialog: AdwaitaWidget {
/// - type: The type of the app storage.
/// - Returns: The view storage.
public func container<Data>(
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
type: Data.Type
) -> ViewStorage where Data: ViewRenderData {
let storage = child.storage(modifiers: modifiers, type: type)
update(storage, modifiers: modifiers, updateProperties: true, type: type)
let storage = child.storage(data: data, type: type)
update(storage, data: data, updateProperties: true, type: type)
return storage
}

Expand All @@ -93,12 +93,12 @@ public struct AlertDialog: AdwaitaWidget {
/// - type: The type of the app storage.
public func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
storage.fields[Self.visibleID + id] = _visible
child.updateStorage(storage, modifiers: modifiers, updateProperties: updateProperties, type: type)
child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
guard updateProperties else {
return
}
Expand Down
18 changes: 9 additions & 9 deletions Sources/Adwaita/View/Dialogs/Dialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ struct Dialog: AdwaitaWidget {
/// - modifiers: Modify views before being updated.
/// - type: The type of the app storage.
/// - Returns: The view storage.
func container<Data>(modifiers: [(AnyView) -> AnyView], type: Data.Type) -> ViewStorage where Data: ViewRenderData {
let child = child.storage(modifiers: modifiers, type: type)
func container<Data>(data: WidgetData, type: Data.Type) -> ViewStorage where Data: ViewRenderData {
let child = child.storage(data: data, type: type)
let storage = ViewStorage(child.opaquePointer, content: [.mainContent: [child]])
update(storage, modifiers: modifiers, updateProperties: true, type: type)
update(storage, data: data, updateProperties: true, type: type)
return storage
}

Expand All @@ -50,23 +50,23 @@ struct Dialog: AdwaitaWidget {
/// - type: The type of the app storage.
func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
if let storage = storage.content[.mainContent]?.first {
child.updateStorage(storage, modifiers: modifiers, updateProperties: updateProperties, type: type)
child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
}
if let storage = storage.content[contentID + id]?.first {
content
.updateStorage(storage, modifiers: modifiers, updateProperties: updateProperties, type: type)
.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
}
guard updateProperties else {
return
}
if visible {
if storage.content[dialogID + id]?.first == nil {
createDialog(storage: storage, modifiers: modifiers, type: type)
createDialog(storage: storage, data: data, type: type)
adw_dialog_present(
storage.content[dialogID + id]?.first?.opaquePointer?.cast(),
storage.opaquePointer?.cast()
Expand Down Expand Up @@ -96,13 +96,13 @@ struct Dialog: AdwaitaWidget {
/// - type: The view render data type.
func createDialog<Data>(
storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
type: Data.Type
) where Data: ViewRenderData {
let pointer = adw_dialog_new()
let dialog = ViewStorage(pointer?.opaque())
storage.content[dialogID + id] = [dialog]
let contentStorage = content.storage(modifiers: modifiers, type: type)
let contentStorage = content.storage(data: data, type: type)
adw_dialog_set_child(pointer, contentStorage.opaquePointer?.cast())
storage.content[contentID + id] = [contentStorage]
dialog.connectSignal(name: "closed") {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Adwaita/View/Dialogs/FileDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ struct FileDialog: AdwaitaWidget {
/// - modifiers: Modify views before being updated.
/// - type: The type of the app storage.
/// - Returns: The view storage.
func container<Data>(modifiers: [(AnyView) -> AnyView], type: Data.Type) -> ViewStorage where Data: ViewRenderData {
let child = child.storage(modifiers: modifiers, type: type)
func container<Data>(data: WidgetData, type: Data.Type) -> ViewStorage where Data: ViewRenderData {
let child = child.storage(data: data, type: type)
let storage = ViewStorage(child.opaquePointer, content: [.mainContent: [child]])
update(storage, modifiers: modifiers, updateProperties: true, type: type)
update(storage, data: data, updateProperties: true, type: type)
return storage
}

Expand All @@ -48,7 +48,7 @@ struct FileDialog: AdwaitaWidget {
/// - type: The type of the app storage.
func update<Data>(
_ storage: ViewStorage,
modifiers: [(AnyView) -> AnyView],
data: WidgetData,
updateProperties: Bool,
type: Data.Type
) where Data: ViewRenderData {
Expand All @@ -57,7 +57,7 @@ struct FileDialog: AdwaitaWidget {
guard let mainStorage = storage.content[.mainContent]?.first else {
return
}
child.updateStorage(storage, modifiers: modifiers, updateProperties: updateProperties, type: type)
child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
if open.update, storage.fields["callbacks"] == nil {
let pointer = gtk_file_dialog_new()
if let initialName {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Adwaita/View/Fixed+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extension Fixed {
@ViewBuilder view: @escaping () -> Body
) -> Self {
var newSelf = self
newSelf.appearFunctions.append { storage, modifiers in
let view = view().storage(modifiers: modifiers, type: AdwaitaMainView.self)
newSelf.appearFunctions.append { storage, data in
let view = view().storage(data: data, type: AdwaitaMainView.self)
gtk_fixed_put(
storage.opaquePointer?.cast(),
view.opaquePointer?.cast(),
Expand All @@ -33,14 +33,14 @@ extension Fixed {
)
storage.content[id] = [view]
}
newSelf.updateFunctions.append { storage, modifiers, updateProperties in
newSelf.updateFunctions.append { storage, data, updateProperties in
guard let content = storage.content[id]?.first else {
return
}
view()
.updateStorage(
content,
modifiers: modifiers,
data: data,
updateProperties: updateProperties,
type: AdwaitaMainView.self
)
Expand Down
Loading

0 comments on commit fa34452

Please sign in to comment.