From 5cd125b0ed82dee8a0422978ef7f8f544dd057d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Wed, 21 Oct 2026 18:00:27 +0300 Subject: [PATCH] build ch --- components/chkgfx/chkgfx.swift | 2 +- ogs-chkgfx/ver-mac/src/ChkgfxApp.swift | 2 +- ogs-chkgfx/ver-mac/src/dialect.swift | 358 +++++++++++++++++++++++++ 3 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 ogs-chkgfx/ver-mac/src/dialect.swift diff --git a/components/chkgfx/chkgfx.swift b/components/chkgfx/chkgfx.swift index fada653..e994fb0 100644 --- a/components/chkgfx/chkgfx.swift +++ b/components/chkgfx/chkgfx.swift @@ -4,7 +4,7 @@ class ChkgfxComponent { init() { Self.singleton = self - chkgfxRegisterEffects(ctrl) + //chkgfxRegisterEffects(ctrl) chkgfxRegisterShoulds(ctrl) } diff --git a/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift b/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift index 1aeaeff..105f2dc 100644 --- a/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift +++ b/ogs-chkgfx/ver-mac/src/ChkgfxApp.swift @@ -10,7 +10,7 @@ struct ChkgfxApp: App { ZStack { RenderView() .ignoresSafeArea() - ChkgfxView(appDelegate.rootVM) + ChkgfxView(appDelegate.chkgfxVM) } } .defaultSize(width: 640, height: 480) diff --git a/ogs-chkgfx/ver-mac/src/dialect.swift b/ogs-chkgfx/ver-mac/src/dialect.swift new file mode 100644 index 0000000..d76883c --- /dev/null +++ b/ogs-chkgfx/ver-mac/src/dialect.swift @@ -0,0 +1,358 @@ + +// Reusable "none" constant +public let DIALECT_CONTEXT_RECENT_FIELD_NONE = "none" + +// Base protocol for each component's state +public protocol DialectContext { + var recentField: String { get set } + + func field(_ name: String) -> T + func fieldAny(_ name: String) -> Any + mutating func setField(_ name: String, _ value: Any) +} + +// Default implementation of `fieldAny` method for the protocol +public extension DialectContext { + func fieldAny(_ name: String) -> Any { + return field(name) + } +} + +// The engine of the dialect components +public class DialectController { + var callbacks = [(DialectContext) -> Void]() + var context: DialectContext + var functions = [(DialectContext) -> DialectContext]() + var isProcessingQueue = false + var queue = [DialectContext]() + + public init(_ context: DialectContext) { + self.context = context + } + + func executeFunctions() { + let c = queue.removeFirst() + // Keep SSOT: Only allow single field change per should-function + context.recentField = c.recentField + context.setField(c.recentField, c.fieldAny(c.recentField)) + + for f in functions { + let ctx = f(context) + if ctx.recentField != DIALECT_CONTEXT_RECENT_FIELD_NONE { + queue.append(ctx) + } + } + + reportContext() + } + + func processQueue() { + // Prevent recursion. + if isProcessingQueue { + return + } + + isProcessingQueue = true + + while (queue.count > 0) { + executeFunctions() + } + + isProcessingQueue = false + } + + public func registerCallback(_ cb: @escaping (DialectContext) -> Void) { + callbacks.append(cb) + } + + public func registerFieldCallback( + _ fieldName: String, + _ cb: @escaping (DialectContext) -> Void + ) { + callbacks.append({ c in + if c.recentField == fieldName { + cb(c) + } + }) + } + + public func registerFunction(_ f: @escaping (DialectContext) -> DialectContext) { + functions.append(f) + } + + func reportContext() { + for cb in callbacks { + cb(context) + } + } + + public func set( + _ fieldName: String, + _ value: Any + ) { + var c = context + c.setField(fieldName, value) + c.recentField = fieldName + queue.append(c) + processQueue() + } +} + +// Register several oneliner callbacks to a controller +func registerOneliners( + _ ctrl: DialectController, + _ items: [Any] +) -> T? { + let halfCount = items.count / 2 + for i in 0.. Void + ctrl.registerFieldCallback(field) { cc in + let c = cc as! T + callback(c) + } + } + + // A hack for generics to operate + return nil +} + +// Context field names for static type check +public struct F { + public static let backingPixelSize = "backingPixelSize" + public static let didClickIncrement = "didClickIncrement" + public static let didClickLoadModel = "didClickLoadModel" + public static let didLaunch = "didLaunch" + public static let didPrepareGL = "didPrepareGL" + public static let didSetup = "didSetup" + public static let pixelSize = "pixelSize" + public static let reshapePixelSize = "reshapePixelSize" + public static let willDeinit = "willDeinit" + +} +public struct ChkgfxContext: DialectContext { + public var didClickLoadModel = Bool() + public var didLaunch = Bool() + public var didSetup = Bool() + + public var recentField = "" + + public func field(_ name: String) -> T { + if (name == "didClickLoadModel") { + return didClickLoadModel as! T + } + else if (name == "didLaunch") { + return didLaunch as! T + } + else if (name == "didSetup") { + return didSetup as! T + } + + return "unknown-field-name" as! T + } + + public mutating func setField( + _ name: String, + _ value: Any + ) { + if (name == "didClickLoadModel") { + didClickLoadModel = value as! Bool + } + else if (name == "didLaunch") { + didLaunch = value as! Bool + } + else if (name == "didSetup") { + didSetup = value as! Bool + } + + } +} + +public struct RenderContext: DialectContext { + public var backingPixelSize = [Int32]() + public var didClickIncrement = Bool() + public var didLaunch = Bool() + public var didPrepareGL = Bool() + public var didSetup = Bool() + public var pixelSize = [Int32]() + public var reshapePixelSize = [Int32]() + public var willDeinit = Bool() + + public var recentField = "" + + public func field(_ name: String) -> T { + if (name == "backingPixelSize") { + return backingPixelSize as! T + } + else if (name == "didClickIncrement") { + return didClickIncrement as! T + } + else if (name == "didLaunch") { + return didLaunch as! T + } + else if (name == "didPrepareGL") { + return didPrepareGL as! T + } + else if (name == "didSetup") { + return didSetup as! T + } + else if (name == "pixelSize") { + return pixelSize as! T + } + else if (name == "reshapePixelSize") { + return reshapePixelSize as! T + } + else if (name == "willDeinit") { + return willDeinit as! T + } + + return "unknown-field-name" as! T + } + + public mutating func setField( + _ name: String, + _ value: Any + ) { + if (name == "backingPixelSize") { + backingPixelSize = value as! [Int32] + } + else if (name == "didClickIncrement") { + didClickIncrement = value as! Bool + } + else if (name == "didLaunch") { + didLaunch = value as! Bool + } + else if (name == "didPrepareGL") { + didPrepareGL = value as! Bool + } + else if (name == "didSetup") { + didSetup = value as! Bool + } + else if (name == "pixelSize") { + pixelSize = value as! [Int32] + } + else if (name == "reshapePixelSize") { + reshapePixelSize = value as! [Int32] + } + else if (name == "willDeinit") { + willDeinit = value as! Bool + } + + } +} + +public func chkgfxSet( + _ key: String, + _ value: Any +) { + ChkgfxComponent.singleton!.ctrl.set(key, value) +} + +public func renderSet( + _ key: String, + _ value: Any +) { + RenderComponent.singleton!.ctrl.set(key, value) +} + +func chkgfxShouldResetDidLaunch(_ c: ChkgfxContext) -> ChkgfxContext { + var c = c + + /* 1. Only once during the first setup */ + if + c.recentField == F.didSetup && + c.didLaunch == false + { + c.didLaunch = true + c.recentField = F.didLaunch + return c + } + + + c.recentField = DIALECT_CONTEXT_RECENT_FIELD_NONE + return c +} + +func renderShouldResetDidLaunch(_ c: RenderContext) -> RenderContext { + var c = c + + /* 1. Only once during the first setup */ + if + c.recentField == F.didSetup && + c.didLaunch == false + { + c.didLaunch = true + c.recentField = F.didLaunch + return c + } + + + c.recentField = DIALECT_CONTEXT_RECENT_FIELD_NONE + return c +} + +func renderShouldResetPixelSize(_ c: RenderContext) -> RenderContext { + var c = c + + /* 1. Upon launch */ + if + c.recentField == F.didLaunch + { + c.pixelSize = [1, 1] + c.recentField = F.pixelSize + return c + } + + /* 2. Upon resize */ + if + c.recentField == F.reshapePixelSize + { + c.pixelSize = c.reshapePixelSize + c.recentField = F.pixelSize + return c + } + + /* 3. Upon DPI change */ + if + c.recentField == F.backingPixelSize + { + c.pixelSize = c.backingPixelSize + c.recentField = F.pixelSize + return c + } + + + c.recentField = DIALECT_CONTEXT_RECENT_FIELD_NONE + return c +} + +func chkgfxRegisterShoulds(_ ctrl: DialectController) { + [ + chkgfxShouldResetDidLaunch, + + ].forEach { f in + ctrl.registerFunction { c in f(c as! ChkgfxContext) } + } +} + +func renderRegisterShoulds(_ ctrl: DialectController) { + [ + renderShouldResetDidLaunch, + renderShouldResetPixelSize, + + ].forEach { f in + ctrl.registerFunction { c in f(c as! RenderContext) } + } +} + +func renderRegisterEffects(_ ctrl: DialectController) { + let _: RenderContext? = registerOneliners(ctrl, [ + F.didClickIncrement, { (c: RenderContext) in renderLoadCubeModel(RenderVM.shared!) }, + F.didPrepareGL, { (c: RenderContext) in renderInit(RenderVM.shared!) }, + F.didPrepareGL, { (c: RenderContext) in RenderVM.shared!.didPrepareGL = true }, + F.didPrepareGL, { (c: RenderContext) in renderStartTimer(RenderVM.shared!) }, + F.pixelSize, { (c: RenderContext) in RenderVM.shared!.pixelSize = c.pixelSize }, + F.willDeinit, { (c: RenderContext) in renderDeinit(RenderVM.shared!) }, + + ]) +}