Skip to content

Commit

Permalink
ListView sample with code only and ListItem template
Browse files Browse the repository at this point in the history
  • Loading branch information
kashifsoofi committed Feb 15, 2024
1 parent cf24fd8 commit 4a5346d
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/GirCore.Libs.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"Samples\\Gtk-4.0\\Builder\\Builder.csproj",
"Samples\\Gtk-4.0\\DrawingArea\\DrawingArea.csproj",
"Samples\\Gtk-4.0\\FontDialog\\FontDialog.csproj",
"Samples\\Gtk-4.0\\ListView\\ListView.csproj",
"Samples\\Gtk-4.0\\Window\\Window.csproj",
"Samples\\GtkSource-5\\GtkSourceView\\GtkSourceView.csproj",
"Samples\\WebKit-6.0\\JavascriptCall\\JavascriptCall.csproj",
Expand Down
15 changes: 15 additions & 0 deletions src/GirCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FontDialog", "Samples\Gtk-4
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Samples\Gtk-4.0\Builder\Builder.csproj", "{48FEE015-C1AD-41EE-A39D-E441F03AC5A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ListView", "Samples\Gtk-4.0\ListView\ListView.csproj", "{3F6B722F-5192-46BC-A389-5E40ABA9041B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -843,6 +845,18 @@ Global
{48FEE015-C1AD-41EE-A39D-E441F03AC5A3}.Release|x64.Build.0 = Release|Any CPU
{48FEE015-C1AD-41EE-A39D-E441F03AC5A3}.Release|x86.ActiveCfg = Release|Any CPU
{48FEE015-C1AD-41EE-A39D-E441F03AC5A3}.Release|x86.Build.0 = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Debug|x64.ActiveCfg = Debug|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Debug|x64.Build.0 = Debug|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Debug|x86.ActiveCfg = Debug|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Debug|x86.Build.0 = Debug|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|Any CPU.Build.0 = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|x64.ActiveCfg = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|x64.Build.0 = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|x86.ActiveCfg = Release|Any CPU
{3F6B722F-5192-46BC-A389-5E40ABA9041B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BF7F9B0B-CB43-4161-BFAD-C6EE479FC86B} = {386AE10F-B7AC-4C97-AC5C-202D3662A868}
Expand Down Expand Up @@ -915,5 +929,6 @@ Global
{BE2159DE-B264-4C71-9B52-94C5C956F76A} = {B670D679-3B2C-43E8-9F87-5E4E17628011}
{B75DC27B-C90A-47E6-909F-C55D6204F35D} = {7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}
{48FEE015-C1AD-41EE-A39D-E441F03AC5A3} = {7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}
{3F6B722F-5192-46BC-A389-5E40ABA9041B} = {7B70E5ED-C5E7-4F32-A458-E5A10F39DA00}
EndGlobalSection
EndGlobal
56 changes: 56 additions & 0 deletions src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace ListViewSample;

using Gtk;
using static Gtk.SignalListItemFactory;

public class CodeListViewWindow : Window
{
public CodeListViewWindow()
: base()
{
Title = "Code ListView";
SetDefaultSize(300, 300);

var stringList = StringList.New(["One", "Two", "Three", "Four"]);

Check failure on line 14 in src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 14 in src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 14 in src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Linux)

The feature 'collection literals' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

Check failure on line 14 in src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Linux)

The feature 'collection literals' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

Check failure on line 14 in src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Windows)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 14 in src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Windows)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.
var selectionModel = NoSelection.New(stringList);
var listItemFactory = SignalListItemFactory.New();
listItemFactory.OnSetup += SetupSignalHandler;
listItemFactory.OnBind += BindSignalHandler;

var listView = ListView.New(selectionModel, listItemFactory);

var scrolledWindow = ScrolledWindow.New();
scrolledWindow.Child = listView;
Child = scrolledWindow;
}

private void SetupSignalHandler(SignalListItemFactory sender, SetupSignalArgs args)
{
var listItem = args.Object as ListItem;
if (listItem is null)
{
return;
}

var label = Label.New(null);
listItem.Child = label;
}

private void BindSignalHandler(SignalListItemFactory sender, BindSignalArgs args)
{
var listItem = args.Object as ListItem;
if (listItem is null)
{
return;
}

var label = listItem.Child as Label;
if (label is null)
{
return;
}

var item = listItem.Item as StringObject;
label.SetText(item?.String ?? "NOT FOUND");
}
}
13 changes: 13 additions & 0 deletions src/Samples/Gtk-4.0/ListView/ListItemTemplate.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<interface>
<template class="GtkListItem">
<property name="child">
<object class="GtkLabel">
<binding name="label">
<lookup name="string" type="GtkStringObject">
<lookup name="item">GtkListItem</lookup>
</lookup>
</binding>
</object>
</property>
</template>
</interface>
21 changes: 21 additions & 0 deletions src/Samples/Gtk-4.0/ListView/ListView.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Remove="**\*.ui" />
<EmbeddedResource Include="**/*.ui">
<LogicalName>%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Libs\Gtk-4.0\Gtk-4.0.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions src/Samples/Gtk-4.0/ListView/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var application = Gtk.Application.New("org.kashif-code-samples.listview.sample", Gio.ApplicationFlags.FlagsNone);
application.OnActivate += (sender, args) =>
{
var buttonShowCodeListView = CreateButton("Show Code ListView Window");
buttonShowCodeListView.OnClicked += (_, _) => new ListViewSample.CodeListViewWindow().Show();

var buttonShowTemplateListView = CreateButton("Show Template ListView Window");
buttonShowTemplateListView.OnClicked += (_, _) => new ListViewSample.TemplateListViewWindow().Show();

var gtkBox = Gtk.Box.New(Gtk.Orientation.Vertical, 0);
gtkBox.Append(buttonShowCodeListView);
gtkBox.Append(buttonShowTemplateListView);

var window = Gtk.ApplicationWindow.New((Gtk.Application) sender);
window.Title = "ListView Sample";
window.SetDefaultSize(300, 300);
window.Child = gtkBox;
window.Show();
};
return application.RunWithSynchronizationContext(null);

static Gtk.Button CreateButton(string label)
{
var button = Gtk.Button.New();
button.Label = label;
button.SetMarginTop(12);
button.SetMarginBottom(12);
button.SetMarginStart(12);
button.SetMarginEnd(12);
return button;
}
27 changes: 27 additions & 0 deletions src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace ListViewSample;

using System.Reflection;
using Gtk;
using GObject;
using GLib;

public class TemplateListViewWindow : Window
{
public TemplateListViewWindow()
: base()
{
Title = "Template ListView";
SetDefaultSize(300, 300);

var stringList = StringList.New(["One", "Two", "Three", "Four"]);

Check failure on line 16 in src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 16 in src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (MacOS)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 16 in src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Linux)

The feature 'collection literals' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

Check failure on line 16 in src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Linux)

The feature 'collection literals' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.

Check failure on line 16 in src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Windows)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 16 in src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs

View workflow job for this annotation

GitHub Actions / Build (Windows)

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.
var selectionModel = SingleSelection.New(stringList);
var bytes = Assembly.GetExecutingAssembly()
.ReadResourceAsByteArray("ListItemTemplate.ui");
var listItemFactory = BuilderListItemFactory.NewFromBytes(null, Bytes.New(bytes));
var listView = ListView.New(selectionModel, listItemFactory);

var scrolledWindow = ScrolledWindow.New();
scrolledWindow.Child = listView;
Child = scrolledWindow;
}
}

0 comments on commit 4a5346d

Please sign in to comment.