Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first attempt at updating lists. Certainly not good. #59

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.CmdPal.Extensions;
using Microsoft.CmdPal.Extensions.Helpers;

namespace SamplePagesExtension;

internal sealed partial class SampleUpdateListPage : DynamicListPage
{
private bool _items;

public SampleUpdateListPage()
{
Icon = new(string.Empty);
Name = "SSH Keychain";

// Start the background task to update the Items boolean every 5 seconds
StartPeriodicUpdate();
}

// This method starts a Task that runs in the background and updates Items every 5 seconds
private void StartPeriodicUpdate()
{
Task.Run(async () =>
{
while (true)
{
await Task.Delay(5000); // Wait for 5 seconds

// Toggle the Items boolean
_items = !_items;
this.Items = _items;
}
});
}

public override ISection[] GetItems(string query)
{
// Change the list items based on the value of _items
var listItems = _items
? new List<ListItem>
{
new(new NoOpCommand()) { Title = "Items is TRUE - Item 1", Subtitle = "This is dynamically generated content" },
new(new NoOpCommand()) { Title = "Items is TRUE - Item 2", Subtitle = "This list updates every 5 seconds" },
}
: new List<ListItem>
{
new(new NoOpCommand()) { Title = "Items is FALSE - Item 1", Subtitle = "Items is currently false" },
new(new NoOpCommand()) { Title = "Items is FALSE - Item 2", Subtitle = "The content will change again in 5 seconds" },
};

return new ISection[]
{
new ListSection()
{
Title = "Updating List Section",
Items = listItems.ToArray(),
},
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"profiles": {
"SamplePagesExtension (Package)": {
"commandName": "MsixPackage"
"commandName": "MsixPackage",
"commandLineArgs": "-RegisterProcessAsComServer"
},
"SamplePagesExtension (Unpackaged)": {
"commandName": "Project"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public partial class SamplePagesCommandsProvider : ICommandProvider
{
Title = "Dynamic List Page Command",
Subtitle = "SamplePages Extension",
},
new ListItem(new SampleUpdateListPage())
{
Title = "Updating List Page Command",
}
];

Expand Down
42 changes: 41 additions & 1 deletion src/modules/cmdpal/WindowsCommandPalette/PageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.ComponentModel;
using Microsoft.CmdPal.Extensions;
using Microsoft.UI.Dispatching;
using Windows.Foundation;

namespace DeveloperCommandPalette;

public class PageViewModel
public class PageViewModel : INotifyPropertyChanged
{
private readonly DispatcherQueue dispatcherQueue;

public event PropertyChangedEventHandler? PropertyChanged;

public bool Nested { get; set; }

public IPage PageAction { get; }

private bool items;

public bool Items
{
get => items;
set
{
items = value;
BubbleXamlPropertyChanged(nameof(items));
}
}

// public IPage PageAction { get => pageAction; set => pageAction = value; }
public ActionViewModel Command { get; }

Expand All @@ -24,8 +42,30 @@ public class PageViewModel

protected PageViewModel(IPage page)
{
page.PropChanged += Page_PropertyChanged;
PageAction = page;
Command = new(page);

this.dispatcherQueue = DispatcherQueue.GetForCurrentThread();
}

private void Page_PropertyChanged(object sender, PropChangedEventArgs args)
{
BubbleXamlPropertyChanged(args.PropertyName);
}

private void BubbleXamlPropertyChanged(string propertyName)
{
if (this.dispatcherQueue == null)
{
// this is highly unusual
return;
}

this.dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
this.PropertyChanged?.Invoke(this, new(propertyName));
});
}

public void DoAction(ActionViewModel action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ResourceDictionary>
<local:StringNotEmptyToVisibilityConverter x:Key="StringNotEmptyToVisibilityConverter" />

<CollectionViewSource x:Name="ItemsCVS" IsSourceGrouped="True" />
<CollectionViewSource x:Name="ItemsCVS" IsSourceGrouped="True"/>
<StackLayout x:Name="HorizontalStackLayout" Orientation="Horizontal" Spacing="8"/>


Expand Down
31 changes: 31 additions & 0 deletions src/modules/cmdpal/WindowsCommandPalette/Views/ListPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
return;
}

ViewModel.PropertyChanged += ViewModel_PropertyChanged;

if (e.NavigationMode == NavigationMode.New)
{
ViewModel.InitialRender().ContinueWith((t) =>
Expand Down Expand Up @@ -212,6 +214,35 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
}
}

private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// Check if the property changed is 'Items'
if (e.PropertyName == nameof(ViewModel.Items))
{
if (ViewModel != null)
{
// Call UpdateFilter when 'Items' changes
DispatcherQueue.TryEnqueue(async () =>
{
await ViewModel.UpdateListItems();
if (ItemsList.Items.Count > 0)
{
ItemsList.SelectedIndex = 0;
}
});
}
}
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
if (ViewModel != null)
{
ViewModel.PropertyChanged -= ViewModel_PropertyChanged;
}
}

private void FilterBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Handled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class ListPage : Page, IListPage
private string _placeholderText = string.Empty;
private string _searchText = string.Empty;
private bool _showDetails;
private bool _items;
private IFilters? _filters;
private IGridProperties? _gridProperties;

Expand All @@ -28,6 +29,16 @@ public string SearchText
}
}

public bool Items
{
get => _items;
set
{
_items = value;
OnPropertyChanged(nameof(Items));
}
}

public bool ShowDetails
{
get => _showDetails;
Expand Down