This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
574aca2
commit 9f83b23
Showing
157 changed files
with
3,515 additions
and
4,675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// MenuCollection.swift | ||
// Adwaita | ||
// | ||
// Created by david-swift on 02.08.2024. | ||
// | ||
|
||
import CAdw | ||
import Foundation | ||
|
||
/// A collection of menus. | ||
public struct MenuCollection: MenuWidget, Wrapper { | ||
|
||
/// The content of the collection. | ||
var content: Body | ||
|
||
/// Initialize a menu. | ||
/// - Parameter content: The content of the collection. | ||
public init(@ViewBuilder content: @escaping () -> Body) { | ||
self.content = content() | ||
} | ||
|
||
/// The view storage. | ||
/// - Parameters: | ||
/// - modifiers: Modify the views before updating. | ||
/// - type: The type of the views. | ||
/// - Returns: The view storage. | ||
public func container<Data>( | ||
modifiers: [(any AnyView) -> any AnyView], | ||
type: Data.Type | ||
) -> ViewStorage where Data: ViewRenderData { | ||
let storages = content.storages(modifiers: modifiers, type: type) | ||
return .init(nil, content: [.mainContent: storages]) | ||
} | ||
|
||
/// Update the stored content. | ||
/// - Parameters: | ||
/// - storage: The storage to update. | ||
/// - modifiers: Modify the views before updating. | ||
/// - updateProperties: Whether to update the properties. | ||
/// - type: The type of the views. | ||
public func update<Data>( | ||
_ storage: ViewStorage, | ||
modifiers: [(AnyView) -> AnyView], | ||
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) | ||
} | ||
|
||
/// Render the collection as a menu. | ||
/// - Parameters: | ||
/// - app: The app object. | ||
/// - window: The window object. | ||
/// - Returns: The view storage with the GMenu as the pointer. | ||
public func getMenu(app: AdwaitaApp, window: AdwaitaWindow?) -> ViewStorage { | ||
let menu = g_menu_new() | ||
let storage = container(modifiers: [], type: MenuContext.self) | ||
initializeMenu(menu: menu, storage: storage, app: app, window: window) | ||
storage.pointer = menu | ||
return storage | ||
} | ||
|
||
/// Initialize a menu. | ||
/// - Parameters: | ||
/// - menu: The pointer to the GMenu. | ||
/// - storage: The storage for the menu's content. | ||
/// - app: The app object. | ||
/// - window: The window object. | ||
func initializeMenu(menu: OpaquePointer?, storage: ViewStorage, app: AdwaitaApp, window: AdwaitaWindow?) { | ||
if storage.pointer == nil { | ||
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 | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// MenuContext.swift | ||
// Adwaita | ||
// | ||
// Created by david-swift on 01.08.24. | ||
// | ||
|
||
/// The menu items view context. | ||
public enum MenuContext: ViewRenderData { | ||
|
||
/// The type of the widgets. | ||
public typealias WidgetType = MenuWidget | ||
/// The wrapper type. | ||
public typealias WrapperType = MenuCollection | ||
/// The either view type. | ||
public typealias EitherViewType = MenuEitherView | ||
|
||
} | ||
|
||
/// The type of the widgets. | ||
public protocol MenuWidget: Meta.Widget { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// MenuEitherView.swift | ||
// Adwaita | ||
// | ||
// Created by david-swift on 06.08.2024. | ||
// | ||
|
||
/// Show one of two views depending on a condition. | ||
public struct MenuEitherView: Meta.EitherView, SimpleView { | ||
|
||
/// The view. | ||
public var view: Body | ||
|
||
/// Initialize an either view. | ||
/// - Parameters: | ||
/// - condition: The condition. | ||
/// - view1: The first view. | ||
/// - view2: The second view. | ||
public init(_ condition: Bool, view1: () -> Body, else view2: () -> Body) { | ||
self.view = condition ? view1() : view2() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,60 @@ | ||
// | ||
// Submenu.swift | ||
// MenuButton.swift | ||
// Adwaita | ||
// | ||
// Created by david-swift on 22.10.23. | ||
// | ||
|
||
import CAdw | ||
|
||
/// A section for menus. | ||
public struct MenuSection: MenuItem { | ||
/// A button widget for menus. | ||
public struct MenuSection: MenuWidget { | ||
|
||
/// The content of the section. | ||
var sectionContent: MenuContent | ||
var sectionContent: Body | ||
|
||
/// Initialize a section for menus. | ||
/// - Parameter content: The content of the section. | ||
public init(@MenuBuilder content: () -> MenuContent) { | ||
public init(@ViewBuilder content: () -> Body) { | ||
self.sectionContent = content() | ||
} | ||
|
||
/// Add the section to a menu. | ||
/// The view storage. | ||
/// - Parameters: | ||
/// - menu: The menu. | ||
/// - app: The application containing the menu. | ||
/// - window: The application window containing the menu. | ||
public func addMenuItem(menu: OpaquePointer?, app: GTUIApp, window: GTUIApplicationWindow?) { | ||
let section = g_menu_new() | ||
g_menu_append_section(menu, nil, section?.cast()) | ||
for element in sectionContent { | ||
element.addMenuItems(menu: section, app: app, window: window) | ||
/// - modifiers: Modify the views before updating. | ||
/// - type: The type of the views. | ||
/// - Returns: The view storage. | ||
public func container<Data>( | ||
modifiers: [(any AnyView) -> any AnyView], | ||
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 | ||
return storage | ||
} | ||
|
||
/// Update the stored content. | ||
/// - Parameters: | ||
/// - storage: The storage to update. | ||
/// - modifiers: Modify the views before updating. | ||
/// - updateProperties: Whether to update the properties. | ||
/// - type: The type of the views. | ||
public func update<Data>( | ||
_ storage: ViewStorage, | ||
modifiers: [(AnyView) -> AnyView], | ||
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) | ||
} | ||
|
||
} |
Oops, something went wrong.