-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from fwcd/navigation
Add navigation views
- Loading branch information
Showing
49 changed files
with
752 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#![feature(type_alias_impl_trait, impl_trait_in_assoc_type)] | ||
|
||
use nuit::{prelude::*, List, NavigationLink, NavigationSplitView, NavigationStack, Text, VStack}; | ||
|
||
#[derive(Bind, Default)] | ||
struct NavigationContent { | ||
i: i32, | ||
} | ||
|
||
impl NavigationContent { | ||
pub const fn new(i: i32) -> Self { | ||
Self { i } | ||
} | ||
} | ||
|
||
impl View for NavigationContent { | ||
type Body = impl View; | ||
|
||
fn body(&self) -> Self::Body { | ||
let i = self.i; | ||
VStack::from(( | ||
Text::new(format!("This is page {i}")), | ||
NavigationLink::with_text("Next", i + 1), | ||
)) | ||
.navigation_destination(Self::new) | ||
} | ||
} | ||
|
||
#[derive(Bind)] | ||
struct NavigationView; | ||
|
||
impl View for NavigationView { | ||
type Body = impl View; | ||
|
||
fn body(&self) -> Self::Body { | ||
NavigationSplitView::with_sidebar( | ||
List::from(( | ||
Text::new("Hello"), | ||
Text::new("World"), | ||
)) | ||
).with_detail( | ||
NavigationStack::from( | ||
NavigationContent::new(0) | ||
) | ||
) | ||
} | ||
} | ||
|
||
fn main() { | ||
nuit::run_app(NavigationView); | ||
} |
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
6 changes: 6 additions & 0 deletions
6
nuit-bridge-swiftui/Sources/NuitBridgeSwiftUI/Event/EventResponse.swift
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,6 @@ | ||
import NuitBridgeSwiftUICore | ||
|
||
enum EventResponse: Codable, Hashable { | ||
case empty | ||
case node(node: Identified<Node>) | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
...i/Sources/NuitBridgeSwiftUICore/Utils/Navigation/NavigationTitleDisplayMode+SwiftUI.swift
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,13 @@ | ||
import SwiftUI | ||
|
||
#if !os(macOS) | ||
public extension NavigationBarItem.TitleDisplayMode { | ||
init(_ displayMode: NavigationTitleDisplayMode) { | ||
switch displayMode { | ||
case .automatic: self = .automatic | ||
case .inline: self = .inline | ||
case .large: self = .large | ||
} | ||
} | ||
} | ||
#endif |
5 changes: 5 additions & 0 deletions
5
...e-swiftui/Sources/NuitBridgeSwiftUICore/Utils/Navigation/NavigationTitleDisplayMode.swift
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,5 @@ | ||
public enum NavigationTitleDisplayMode: String, Codable, Hashable { | ||
case automatic | ||
case inline | ||
case large | ||
} |
103 changes: 103 additions & 0 deletions
103
nuit-bridge-swiftui/Sources/NuitBridgeSwiftUICore/Utils/Value.swift
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,103 @@ | ||
/// An arbitrary JSON value. | ||
public enum Value: Codable, Hashable { | ||
case null | ||
case bool(Bool) | ||
case int(Int) | ||
case double(Double) | ||
case string(String) | ||
case array([Value]) | ||
case object([String: Value]) | ||
|
||
public init(from decoder: any Decoder) throws { | ||
if let value = try? [Value](from: decoder) { | ||
self = .array(value) | ||
} else if let value = try? [String: Value](from: decoder) { | ||
self = .object(value) | ||
} else { | ||
let container = try decoder.singleValueContainer() | ||
if container.decodeNil() { | ||
self = .null | ||
} else if let value = try? container.decode(Bool.self) { | ||
self = .bool(value) | ||
} else if let value = try? container.decode(Int.self) { | ||
self = .int(value) | ||
} else if let value = try? container.decode(Double.self) { | ||
self = .double(value) | ||
} else if let value = try? container.decode(String.self) { | ||
self = .string(value) | ||
} else { | ||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Could not decode JSON") | ||
} | ||
} | ||
} | ||
|
||
public func encode(to encoder: any Encoder) throws { | ||
switch self { | ||
case .null: | ||
var container = encoder.singleValueContainer() | ||
try container.encodeNil() | ||
case .bool(let value): | ||
var container = encoder.singleValueContainer() | ||
try container.encode(value) | ||
case .int(let value): | ||
var container = encoder.singleValueContainer() | ||
try container.encode(value) | ||
case .double(let value): | ||
var container = encoder.singleValueContainer() | ||
try container.encode(value) | ||
case .string(let value): | ||
var container = encoder.singleValueContainer() | ||
try container.encode(value) | ||
case .array(let value): | ||
var container = encoder.singleValueContainer() | ||
try container.encode(value) | ||
case .object(let value): | ||
var container = encoder.singleValueContainer() | ||
try container.encode(value) | ||
} | ||
} | ||
} | ||
|
||
extension Value: ExpressibleByArrayLiteral { | ||
public init(arrayLiteral elements: Value...) { | ||
self = .array(elements) | ||
} | ||
} | ||
|
||
extension Value: ExpressibleByDictionaryLiteral { | ||
public init(dictionaryLiteral elements: (String, Value)...) { | ||
self = .object(Dictionary(uniqueKeysWithValues: elements)) | ||
} | ||
} | ||
|
||
extension Value: ExpressibleByStringLiteral { | ||
public init(stringLiteral value: String) { | ||
self = .string(value) | ||
} | ||
} | ||
|
||
extension Value: ExpressibleByStringInterpolation {} | ||
|
||
extension Value: ExpressibleByBooleanLiteral { | ||
public init(booleanLiteral value: Bool) { | ||
self = .bool(value) | ||
} | ||
} | ||
|
||
extension Value: ExpressibleByIntegerLiteral { | ||
public init(integerLiteral value: Int) { | ||
self = .int(value) | ||
} | ||
} | ||
|
||
extension Value: ExpressibleByFloatLiteral { | ||
public init(floatLiteral value: Double) { | ||
self = .double(value) | ||
} | ||
} | ||
|
||
extension Value: ExpressibleByNilLiteral { | ||
public init(nilLiteral: ()) { | ||
self = .null | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
nuit-bridge-swiftui/Tests/NuitBridgeSwiftUICoreTests/Utils/ValueTests.swift
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,34 @@ | ||
import NuitBridgeSwiftUICore | ||
import SwiftUI | ||
import XCTest | ||
|
||
final class ValueTests: XCTestCase { | ||
func testCoding() throws { | ||
let cases: [(String, Value, UInt)] = [ | ||
("true", true, #line), | ||
("false", false, #line), | ||
("42", 42, #line), | ||
("42.5", 42.5, #line), | ||
("[]", [], #line), | ||
("[1,2,3]", [1, 2, 3], #line), | ||
("[1,\"a\",3.2,null]", [1, "a", 3.2, nil], #line), | ||
("{\"b\":[9,null,null]}", ["b": [9, nil, nil]], #line), | ||
] | ||
|
||
for (json, value, line) in cases { | ||
XCTAssertEqual( | ||
try JSONDecoder().decode(Value.self, from: json.data(using: .utf8)!), | ||
value, | ||
"Could not decode JSON", | ||
line: line | ||
) | ||
|
||
XCTAssertEqual( | ||
String(data: try JSONEncoder().encode(value), encoding: .utf8)!, | ||
json, | ||
"Could not encode to JSON", | ||
line: line | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.