From 57f98b7d61c243c54d66db14f952e93cb41ea989 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sun, 10 Dec 2017 14:21:49 -0800 Subject: [PATCH] Implement SwitchRenderer using Bootstrap Toggle --- Ooui.Forms/Exports.cs | 1 + Ooui.Forms/Renderers/SwitchRenderer.cs | 53 ++++++++++++++++++++++++++ Ooui/CanvasRenderingContext2D.cs | 38 +++++++++--------- Ooui/Client.js | 16 ++++++-- Ooui/Element.cs | 14 +++++++ Ooui/EventTarget.cs | 2 +- Ooui/Node.cs | 6 +-- Ooui/UI.cs | 4 ++ Samples/DisplayAlertPage.xaml | 4 ++ 9 files changed, 112 insertions(+), 26 deletions(-) create mode 100644 Ooui.Forms/Renderers/SwitchRenderer.cs diff --git a/Ooui.Forms/Exports.cs b/Ooui.Forms/Exports.cs index 5388bab..a467bf5 100644 --- a/Ooui.Forms/Exports.cs +++ b/Ooui.Forms/Exports.cs @@ -14,6 +14,7 @@ using Xamarin.Forms.Internals; [assembly: ExportRenderer (typeof (Frame), typeof (FrameRenderer))] [assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))] [assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))] +[assembly: ExportRenderer (typeof (Switch), typeof (SwitchRenderer))] namespace Ooui.Forms { diff --git a/Ooui.Forms/Renderers/SwitchRenderer.cs b/Ooui.Forms/Renderers/SwitchRenderer.cs new file mode 100644 index 0000000..75f1a7f --- /dev/null +++ b/Ooui.Forms/Renderers/SwitchRenderer.cs @@ -0,0 +1,53 @@ +using System; +using Xamarin.Forms; + +namespace Ooui.Forms.Renderers +{ + public class SwitchRenderer : ViewRenderer + { + public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint) + { + var size = new Size (54, 38); + return new SizeRequest (size, size); + } + + protected override void Dispose (bool disposing) + { + if (disposing) + Control.Changed -= OnControlValueChanged; + + base.Dispose (disposing); + } + + protected override void OnElementChanged (ElementChangedEventArgs e) + { + if (e.OldElement != null) + e.OldElement.Toggled -= OnElementToggled; + + if (e.NewElement != null) { + if (Control == null) { + var input = new Input (InputType.Checkbox); + input.SetAttribute ("data-toggle", "toggle"); + SetNativeControl (input); + input.Call ("$.bootstrapToggle"); + Control.Changed += OnControlValueChanged; + } + + Control.IsChecked = Element.IsToggled; + e.NewElement.Toggled += OnElementToggled; + } + + base.OnElementChanged (e); + } + + void OnControlValueChanged (object sender, EventArgs e) + { + ((IElementController)Element).SetValueFromRenderer (Switch.IsToggledProperty, Control.IsChecked); + } + + void OnElementToggled (object sender, EventArgs e) + { + Control.IsChecked = Element.IsToggled; + } + } +} diff --git a/Ooui/CanvasRenderingContext2D.cs b/Ooui/CanvasRenderingContext2D.cs index 210017d..64d80f8 100644 --- a/Ooui/CanvasRenderingContext2D.cs +++ b/Ooui/CanvasRenderingContext2D.cs @@ -81,97 +81,97 @@ namespace Ooui public void Save () { - SendCall ("save"); + Call ("save"); } public void Restore () { - SendCall ("restore"); + Call ("restore"); } public void ClearRect (double x, double y, double w, double h) { - SendCall ("clearRect", x, y, w, h); + Call ("clearRect", x, y, w, h); } public void FillRect (double x, double y, double w, double h) { - SendCall ("fillRect", x, y, w, h); + Call ("fillRect", x, y, w, h); } public void StrokeRect (double x, double y, double w, double h) { - SendCall ("strokeRect", x, y, w, h); + Call ("strokeRect", x, y, w, h); } public void BeginPath () { - SendCall ("beginPath"); + Call ("beginPath"); } public void ClosePath () { - SendCall ("closePath"); + Call ("closePath"); } public void MoveTo (double x, double y) { - SendCall ("moveTo", x, y); + Call ("moveTo", x, y); } public void LineTo (double x, double y) { - SendCall ("lineTo", x, y); + Call ("lineTo", x, y); } public void QuadraticCurveTo (double cpx, double cpy, double x, double y) { - SendCall ("quadraticCurveTo", cpx, cpy, x, y); + Call ("quadraticCurveTo", cpx, cpy, x, y); } public void BezierCurveTo (double cp1x, double cp1y, double cp2x, double cp2y, double x, double y) { - SendCall ("bezierCurveTo", cp1x, cp1y, cp2x, cp2y, x, y); + Call ("bezierCurveTo", cp1x, cp1y, cp2x, cp2y, x, y); } public void ArcTo (double x1, double y1, double x2, double y2, double radius) { - SendCall ("arcTo", x1, y1, x2, y2, radius); + Call ("arcTo", x1, y1, x2, y2, radius); } public void Rect (double x, double y, double w, double h) { - SendCall ("rect", x, y, w, h); + Call ("rect", x, y, w, h); } public void Arc (double x, double y, double radius, double startAngle, double endAngle, bool counterclockwise) { - SendCall ("arc", x, y, radius, startAngle, endAngle, counterclockwise); + Call ("arc", x, y, radius, startAngle, endAngle, counterclockwise); } public void Fill () { - SendCall ("fill"); + Call ("fill"); } public void Stroke () { - SendCall ("stroke"); + Call ("stroke"); } public void Clip () { - SendCall ("clip"); + Call ("clip"); } public void FillText (string text, double x, double y, double? maxWidth) { - SendCall ("fillText", text, x, y, maxWidth); + Call ("fillText", text, x, y, maxWidth); } public void StrokeText (string text, double x, double y, double? maxWidth) { - SendCall ("strokeText", text, x, y, maxWidth); + Call ("strokeText", text, x, y, maxWidth); } } diff --git a/Ooui/Client.js b/Ooui/Client.js index e796069..faac03a 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -65,11 +65,19 @@ function ooui (rootElementPath) { const messages = JSON.parse (event.data); if (debug) console.log("Messages", messages); if (Array.isArray (messages)) { + const jqs = [] messages.forEach (function (m) { // console.log('Raw value from server', m.v); m.v = fixupValue (m.v); - processMessage (m); + if (m.k.startsWith ("$.")) { + jqs.push (m); + } + else { + processMessage (m); + } }); + // Run jQuery functions last since they usually require a fully built DOM + jqs.forEach (processMessage); } }); @@ -163,9 +171,11 @@ function msgCall (m) { console.error ("Unknown node id", m); return; } - const f = node[m.k]; + const isJQuery = m.k.startsWith ("$."); + const target = isJQuery ? $(node) : node; + const f = isJQuery ? target[m.k.slice(2)] : target[m.k]; if (debug) console.log ("Call", node, f, m.v); - const r = f.apply (node, m.v); + const r = f.apply (target, m.v); if (typeof m.rid === 'string' || m.rid instanceof String) { nodes[m.rid] = r; } diff --git a/Ooui/Element.cs b/Ooui/Element.cs index a7287d1..ea87569 100644 --- a/Ooui/Element.cs +++ b/Ooui/Element.cs @@ -116,5 +116,19 @@ namespace Ooui { SendSet ("style." + Style.GetJsName (e.PropertyName), Style[e.PropertyName]); } + + protected override bool SaveStateMessageIfNeeded (Message message) + { + if (message.TargetId != Id) + return false; + + switch (message.MessageType) { + case MessageType.Call when message.Key.StartsWith ("$.", StringComparison.Ordinal): + AddStateMessage (message); + return true; + default: + return base.SaveStateMessageIfNeeded (message); + } + } } } diff --git a/Ooui/EventTarget.cs b/Ooui/EventTarget.cs index a7be894..e3e26c1 100644 --- a/Ooui/EventTarget.cs +++ b/Ooui/EventTarget.cs @@ -115,7 +115,7 @@ namespace Ooui MessageSent?.Invoke (message); } - protected void SendCall (string methodName, params object[] args) + public void Call (string methodName, params object[] args) { Send (Message.Call (Id, methodName, args)); } diff --git a/Ooui/Node.cs b/Ooui/Node.cs index 2443a2b..18d16a0 100644 --- a/Ooui/Node.cs +++ b/Ooui/Node.cs @@ -73,7 +73,7 @@ namespace Ooui } } newChild.MessageSent += HandleChildMessageSent; - SendCall ("insertBefore", newChild, referenceChild); + Call ("insertBefore", newChild, referenceChild); OnChildInsertedBefore (newChild, referenceChild); return newChild; } @@ -88,7 +88,7 @@ namespace Ooui } } child.MessageSent -= HandleChildMessageSent; - SendCall ("removeChild", child); + Call ("removeChild", child); OnChildRemoved (child); return child; } @@ -110,7 +110,7 @@ namespace Ooui } foreach (var child in toRemove) { child.MessageSent -= HandleChildMessageSent; - SendCall ("removeChild", child); + Call ("removeChild", child); } InsertBefore (newNode, null); } diff --git a/Ooui/UI.cs b/Ooui/UI.cs index a422abf..aa0f75e 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -40,10 +40,14 @@ namespace Ooui @Title +
+ + + diff --git a/Samples/DisplayAlertPage.xaml b/Samples/DisplayAlertPage.xaml index ca53abc..63fee3a 100644 --- a/Samples/DisplayAlertPage.xaml +++ b/Samples/DisplayAlertPage.xaml @@ -9,6 +9,10 @@ + + + +