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

ListView sample with code only and ListItem template #1022

Merged
merged 1 commit into from
Feb 21, 2024
Merged
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
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
55 changes: 55 additions & 0 deletions src/Samples/Gtk-4.0/ListView/CodeListViewWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Gtk;
using static Gtk.SignalListItemFactory;

namespace ListViewSample;

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

var stringList = StringList.New(new string[] { "One", "Two", "Three", "Four" });
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>
18 changes: 18 additions & 0 deletions src/Samples/Gtk-4.0/ListView/ListView.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="**/*.ui">
<LogicalName>%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
</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.gir.core", 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;
}
26 changes: 26 additions & 0 deletions src/Samples/Gtk-4.0/ListView/TemplateListViewWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Reflection;
using GLib;
using GObject;
using Gtk;

namespace ListViewSample;

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

var stringList = StringList.New(new string[] { "One", "Two", "Three", "Four" });
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;
}
}
Loading