From 79e4e87b9560a9dbccb2928d8932c44fe0dee24f Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sun, 15 Apr 2018 22:55:23 -0700 Subject: [PATCH] Add some ScrollTo support to ListView --- Ooui.Forms/Extensions/FontExtensions.cs | 2 +- Ooui.Forms/Renderers/ListViewRenderer.cs | 21 +++++++++++++++++++++ Ooui/Client.js | 4 ++++ Ooui/Message.cs | 16 ++++++++++++++++ Samples/ListView/RefreshListView.xaml | 6 ++++-- Samples/ListView/RefreshListView.xaml.cs | 7 +++++++ 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Ooui.Forms/Extensions/FontExtensions.cs b/Ooui.Forms/Extensions/FontExtensions.cs index f4c5860..4776f83 100644 --- a/Ooui.Forms/Extensions/FontExtensions.cs +++ b/Ooui.Forms/Extensions/FontExtensions.cs @@ -77,7 +77,7 @@ namespace Ooui.Forms.Extensions } else { if (c == ' ') { - if (i >= lineStartIndex && text[i-1] != ' ') + if (i >= lineStartIndex && i > 0 && text[i-1] != ' ') firstSpaceX = px; lastSpaceIndex = i; } diff --git a/Ooui.Forms/Renderers/ListViewRenderer.cs b/Ooui.Forms/Renderers/ListViewRenderer.cs index 95b18f1..5dd5482 100644 --- a/Ooui.Forms/Renderers/ListViewRenderer.cs +++ b/Ooui.Forms/Renderers/ListViewRenderer.cs @@ -27,6 +27,7 @@ namespace Ooui.Forms.Renderers { var templatedItems = TemplatedItemsView.TemplatedItems; templatedItems.CollectionChanged -= OnCollectionChanged; + e.OldElement.ScrollToRequested -= ListView_ScrollToRequested; } if (e.NewElement != null) @@ -41,6 +42,7 @@ namespace Ooui.Forms.Renderers var templatedItems = TemplatedItemsView.TemplatedItems; templatedItems.CollectionChanged += OnCollectionChanged; + e.NewElement.ScrollToRequested += ListView_ScrollToRequested; UpdateItems (); UpdateBackgroundColor(); @@ -70,6 +72,7 @@ namespace Ooui.Forms.Renderers { var templatedItems = TemplatedItemsView.TemplatedItems; templatedItems.CollectionChanged -= OnCollectionChanged; + Element.ScrollToRequested -= ListView_ScrollToRequested; } _disposed = true; @@ -139,6 +142,24 @@ namespace Ooui.Forms.Renderers Element.NotifyRowTapped(ndx, null); } + void ListView_ScrollToRequested (object sender, ScrollToRequestedEventArgs e) + { + if (Control == null) + return; + + var oe = (ITemplatedItemsListScrollToRequestedEventArgs)e; + var item = oe.Item; + var group = oe.Group; + switch (e.Position) { + case ScrollToPosition.Start: + Control.Send (Ooui.Message.Set (Control.Id, "scrollTop", 0)); + break; + case ScrollToPosition.End: + Control.Send (Ooui.Message.Set (Control.Id, "scrollTop", new Ooui.Message.PropertyReference { TargetId = Control.Id, Key = "scrollHeight" })); + break; + } + } + private void UpdateBackgroundColor() { var backgroundColor = Element.BackgroundColor.ToOouiColor(); diff --git a/Ooui/Client.js b/Ooui/Client.js index 4b1f998..7a271ef 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -295,6 +295,7 @@ function processMessage (m) { } function fixupValue (v) { + var x, n; if (Array.isArray (v)) { for (x in v) { v[x] = fixupValue (v[x]); @@ -307,6 +308,9 @@ function fixupValue (v) { return getNode (v); } } + else if (!!v && v.hasOwnProperty("id") && v.hasOwnProperty("k")) { + return fixupValue(v["id"])[v["k"]]; + } return v; } diff --git a/Ooui/Message.cs b/Ooui/Message.cs index 5db69af..3aa9c1e 100644 --- a/Ooui/Message.cs +++ b/Ooui/Message.cs @@ -29,6 +29,13 @@ namespace Ooui Value = args, }; + public static Message Set (string targetId, string property, object value) => new Message { + MessageType = MessageType.Set, + TargetId = targetId, + Key = property, + Value = value, + }; + public static Message Event (string targetId, string eventType, object value = null) => new Message { MessageType = MessageType.Event, TargetId = targetId, @@ -36,6 +43,15 @@ namespace Ooui Value = value, }; + public class PropertyReference + { + [JsonProperty ("id")] + public string TargetId = ""; + + [JsonProperty ("k")] + public string Key = ""; + } + public void WriteJson (System.IO.TextWriter w) { w.Write ('{'); diff --git a/Samples/ListView/RefreshListView.xaml b/Samples/ListView/RefreshListView.xaml index 45eb936..2c69c9e 100644 --- a/Samples/ListView/RefreshListView.xaml +++ b/Samples/ListView/RefreshListView.xaml @@ -1,4 +1,4 @@ - + @@ -22,7 +23,8 @@ Text="{Binding Input}" />