Skip to content

Commit 28747b4

Browse files
committed
Port add-in dialog to new subclass init style.
1 parent 440335f commit 28747b4

6 files changed

Lines changed: 166 additions & 94 deletions

File tree

Pinta.Gui.Addins/AddinInfoView.cs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using Mono.Addins;
34
using Pinta.Core;
45

56
namespace Pinta.Gui.Addins;
67

7-
internal sealed class AddinInfoView : Adw.Bin
8+
[GObject.Subclass<Adw.Bin>]
9+
internal sealed partial class AddinInfoView
810
{
9-
private readonly Gtk.Label title_label;
10-
private readonly Gtk.Label version_label;
11-
private readonly Gtk.Label size_label;
12-
private readonly Gtk.Label repo_label;
13-
private readonly Gtk.Label description_label;
11+
private Gtk.Label title_label;
12+
private Gtk.Label version_label;
13+
private Gtk.Label size_label;
14+
private Gtk.Label repo_label;
15+
private Gtk.Label description_label;
1416

15-
private readonly Gtk.Button info_button;
16-
private readonly Gtk.Button install_button;
17-
private readonly Gtk.Button update_button;
18-
private readonly Gtk.Button uninstall_button;
17+
private Gtk.Button info_button;
18+
private Gtk.Button install_button;
19+
private Gtk.Button update_button;
20+
private Gtk.Button uninstall_button;
1921

20-
private readonly Gtk.Switch enable_switch;
22+
private Gtk.Switch enable_switch;
2123

22-
private readonly Gtk.Box content_box;
24+
private Gtk.Box content_box;
2325

24-
private readonly Adw.Bin empty_page;
26+
private Adw.Bin empty_page;
2527

26-
private readonly Adw.ViewStack view_stack;
28+
private Adw.ViewStack view_stack;
2729
private AddinListViewItem? current_item;
2830

2931
/// <summary>
3032
/// Event raised when addins are installed or uninstalled.
3133
/// </summary>
3234
public event EventHandler? OnAddinChanged;
3335

34-
private readonly SystemManager system;
35-
private readonly IChromeService chrome;
36-
37-
public AddinInfoView (SystemManager system, IChromeService chrome)
36+
private SystemManager system = null!; // NRT - set by factory method.
37+
private IChromeService chrome = null!;
38+
39+
[MemberNotNull (nameof (title_label))]
40+
[MemberNotNull (nameof (version_label))]
41+
[MemberNotNull (nameof (size_label))]
42+
[MemberNotNull (nameof (repo_label))]
43+
[MemberNotNull (nameof (description_label))]
44+
[MemberNotNull (nameof (info_button))]
45+
[MemberNotNull (nameof (install_button))]
46+
[MemberNotNull (nameof (update_button))]
47+
[MemberNotNull (nameof (uninstall_button))]
48+
[MemberNotNull (nameof (enable_switch))]
49+
[MemberNotNull (nameof (content_box))]
50+
[MemberNotNull (nameof (empty_page))]
51+
[MemberNotNull (nameof (view_stack))]
52+
partial void Initialize ()
3853
{
3954
// --- Control creation
4055

@@ -129,11 +144,16 @@ public AddinInfoView (SystemManager system, IChromeService chrome)
129144
empty_page = emptyPage;
130145

131146
view_stack = viewStack;
147+
}
132148

149+
internal void Configure (SystemManager system, IChromeService chrome)
150+
{
133151
this.system = system;
134152
this.chrome = chrome;
135153
}
136154

155+
public static new AddinInfoView New () => NewWithProperties ([]);
156+
137157
private Gtk.Switch CreateEnableSwitch ()
138158
{
139159
Gtk.Switch result = Gtk.Switch.New ();
@@ -259,7 +279,7 @@ private void HandleInstallButtonClicked ()
259279
if (current_item.RepositoryEntry is null)
260280
throw new InvalidOperationException ("The install button should not be available unless there is a repository entry");
261281

262-
InstallDialog dialog = new (chrome.MainWindow, current_item.Service);
282+
InstallDialog dialog = InstallDialog.New (chrome.MainWindow, current_item.Service);
263283
dialog.OnSuccess += (_, _) => OnAddinChanged?.Invoke (this, EventArgs.Empty);
264284
dialog.InitForInstall ([current_item.RepositoryEntry]);
265285
dialog.Show ();
@@ -273,7 +293,7 @@ private void HandleUpdateButtonClicked ()
273293
if (current_item.RepositoryEntry is null)
274294
throw new InvalidOperationException ("The update button should not be available unless there is a repository entry");
275295

276-
InstallDialog dialog = new (chrome.MainWindow, current_item.Service);
296+
InstallDialog dialog = InstallDialog.New (chrome.MainWindow, current_item.Service);
277297
dialog.OnSuccess += (_, _) => OnAddinChanged?.Invoke (this, EventArgs.Empty);
278298
dialog.InitForInstall ([current_item.RepositoryEntry]);
279299
dialog.Show ();
@@ -287,7 +307,7 @@ private void HandleUninstallButtonClicked ()
287307
if (current_item.Addin is null)
288308
throw new InvalidOperationException ("The uninstall button should not be available unless there is an installed addin");
289309

290-
InstallDialog dialog = new (chrome.MainWindow, current_item.Service);
310+
InstallDialog dialog = InstallDialog.New (chrome.MainWindow, current_item.Service);
291311
dialog.OnSuccess += (_, _) => OnAddinChanged?.Invoke (this, EventArgs.Empty);
292312
dialog.InitForUninstall ([current_item.Addin]);
293313
dialog.Show ();

Pinta.Gui.Addins/AddinListView.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using Mono.Addins;
34
using Mono.Addins.Setup;
45
using Pinta.Core;
56
using Pinta.Resources;
67

78
namespace Pinta.Gui.Addins;
89

9-
internal sealed class AddinListView : Adw.Bin
10+
[GObject.Subclass<Adw.Bin>]
11+
internal sealed partial class AddinListView
1012
{
11-
private readonly Gio.ListStore model;
12-
private readonly Gtk.SingleSelection selection_model;
13+
private Gio.ListStore model;
14+
private Gtk.SingleSelection selection_model;
1315

14-
private readonly Adw.StatusPage empty_list_page;
15-
private readonly Gtk.ScrolledWindow list_view_scroll;
16-
private readonly Adw.ViewStack list_view_stack;
16+
private Adw.StatusPage empty_list_page;
17+
private Gtk.ScrolledWindow list_view_scroll;
18+
private Adw.ViewStack list_view_stack;
1719

18-
private readonly AddinInfoView info_view;
20+
private AddinInfoView info_view;
1921

2022
/// <summary>
2123
/// Event raised when addins are installed or uninstalled.
2224
/// </summary>
2325
public event EventHandler? OnAddinChanged;
2426

25-
public AddinListView (SystemManager system, IChromeService chrome)
27+
[MemberNotNull (nameof (model), nameof (selection_model))]
28+
[MemberNotNull (nameof (empty_list_page), nameof (list_view_scroll), nameof (list_view_stack))]
29+
[MemberNotNull (nameof (info_view))]
30+
partial void Initialize ()
2631
{
2732
Gio.ListStore listStore = Gio.ListStore.New (AddinListViewItem.GetGType ());
2833

@@ -33,7 +38,7 @@ public AddinListView (SystemManager system, IChromeService chrome)
3338
Gtk.SignalListItemFactory itemFactory = Gtk.SignalListItemFactory.New ();
3439
itemFactory.OnSetup += (factory, args) => {
3540
var item = (Gtk.ListItem) args.Object;
36-
item.SetChild (new AddinListViewItemWidget ());
41+
item.SetChild (AddinListViewItemWidget.New ());
3742
};
3843
itemFactory.OnBind += (factory, args) => {
3944
var list_item = (Gtk.ListItem) args.Object;
@@ -59,7 +64,7 @@ public AddinListView (SystemManager system, IChromeService chrome)
5964
listViewStack.Add (listViewScroll);
6065
listViewStack.Add (emptyListPage);
6166

62-
AddinInfoView infoView = new (system, chrome);
67+
AddinInfoView infoView = AddinInfoView.New ();
6368
infoView.OnAddinChanged += (o, e) => OnAddinChanged?.Invoke (o, e);
6469

6570
Adw.Flap flap = Adw.Flap.New ();
@@ -84,6 +89,17 @@ public AddinListView (SystemManager system, IChromeService chrome)
8489
SetChild (flap);
8590
}
8691

92+
internal void Configure (SystemManager system, IChromeService chrome)
93+
{
94+
info_view.Configure (system, chrome);
95+
}
96+
97+
public static new AddinListView New ()
98+
{
99+
AddinListView view = NewWithProperties ([]);
100+
return view;
101+
}
102+
87103
public void Clear ()
88104
{
89105
model.RemoveAll ();

Pinta.Gui.Addins/AddinListViewItem.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using GObject;
23
using Mono.Addins;
34
using Mono.Addins.Setup;
@@ -84,12 +85,15 @@ public string? DownloadSize {
8485
!string.IsNullOrEmpty (available_addin.RepositoryName) ? available_addin.RepositoryName : available_addin.RepositoryUrl;
8586
}
8687

87-
internal sealed class AddinListViewItemWidget : Gtk.Box
88+
[GObject.Subclass<Gtk.Box>]
89+
internal sealed partial class AddinListViewItemWidget
8890
{
89-
private readonly Gtk.Label name_label;
90-
private readonly Gtk.Label description_label;
91+
private Gtk.Label name_label;
92+
private Gtk.Label description_label;
9193

92-
public AddinListViewItemWidget ()
94+
[MemberNotNull (nameof (name_label))]
95+
[MemberNotNull (nameof (description_label))]
96+
partial void Initialize ()
9397
{
9498
Gtk.Label nameLabel = Gtk.Label.New (null);
9599
nameLabel.Halign = Gtk.Align.Start;
@@ -120,6 +124,8 @@ public AddinListViewItemWidget ()
120124
Append (descriptionLabel);
121125
}
122126

127+
public static AddinListViewItemWidget New () => NewWithProperties ([]);
128+
123129
// Set the widget's contents to the provided item.
124130
public void Update (AddinListViewItem item)
125131
{

Pinta.Gui.Addins/AddinManagerDialog.cs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Mono.Addins;
@@ -9,21 +10,20 @@
910

1011
namespace Pinta.Gui.Addins;
1112

12-
public sealed class AddinManagerDialog : Adw.Window
13+
[GObject.Subclass<Adw.Window>]
14+
public sealed partial class AddinManagerDialog
1315
{
14-
private readonly SetupService setup_service;
16+
private SetupService setup_service = null!; // NRT - set by factory method
1517

16-
private readonly AddinListView installed_list;
17-
private readonly AddinListView updates_list;
18-
private readonly AddinListView gallery_list;
18+
private AddinListView installed_list;
19+
private AddinListView updates_list;
20+
private AddinListView gallery_list;
1921

20-
private readonly StatusProgressBar progress_bar;
22+
private StatusProgressBar progress_bar;
2123

22-
public AddinManagerDialog (
23-
Gtk.Window parent,
24-
SetupService service,
25-
SystemManager system,
26-
IChromeService chrome)
24+
[MemberNotNull (nameof (installed_list), nameof (updates_list), nameof (gallery_list))]
25+
[MemberNotNull (nameof (progress_bar))]
26+
partial void Initialize ()
2727
{
2828
// TODO - add a dialog for managing the list of repositories.
2929
// TODO - support searching through the gallery
@@ -33,9 +33,9 @@ public AddinManagerDialog (
3333
Gtk.Button installFileButton = CreateInstallFileButton ();
3434
Gtk.Button refreshButton = CreateRefreshButton ();
3535

36-
AddinListView galleryList = CreateAddinList (system, chrome);
37-
AddinListView installedList = CreateAddinList (system, chrome);
38-
AddinListView updatesList = CreateAddinList (system, chrome);
36+
AddinListView galleryList = CreateAddinList ();
37+
AddinListView installedList = CreateAddinList ();
38+
AddinListView updatesList = CreateAddinList ();
3939

4040
Adw.ViewStack viewStack = CreateViewStack (galleryList, installedList, updatesList);
4141

@@ -52,10 +52,6 @@ public AddinManagerDialog (
5252
headerBar.PackStart (installFileButton);
5353
headerBar.PackStart (refreshButton);
5454

55-
// --- Property assignment (GTK window)
56-
57-
TransientFor = parent;
58-
5955
// --- Property assignment (Adwaita window)
6056

6157
Content = GtkExtensions.BoxVertical ([
@@ -64,19 +60,40 @@ public AddinManagerDialog (
6460

6561
// --- References to keep
6662

67-
this.setup_service = service;
68-
6963
progress_bar = progressBar;
7064

7165
gallery_list = galleryList;
7266
installed_list = installedList;
7367
updates_list = updatesList;
68+
}
7469

75-
// --- Post-initialization
70+
private void Configure (
71+
Gtk.Window parent,
72+
SetupService service,
73+
SystemManager system,
74+
IChromeService chrome)
75+
{
76+
TransientFor = parent;
77+
this.setup_service = service;
78+
installed_list.Configure (system, chrome);
79+
updates_list.Configure (system, chrome);
80+
gallery_list.Configure (system, chrome);
7681

82+
// --- Post-initialization
7783
LoadAll ();
7884
}
7985

86+
public static AddinManagerDialog New (
87+
Gtk.Window parent,
88+
SetupService service,
89+
SystemManager system,
90+
IChromeService chrome)
91+
{
92+
AddinManagerDialog dialog = NewWithProperties ([]);
93+
dialog.Configure (parent, service, system, chrome);
94+
return dialog;
95+
}
96+
8097
private static Adw.ViewStack CreateViewStack (
8198
AddinListView galleryList,
8299
AddinListView installedList,
@@ -89,9 +106,9 @@ private static Adw.ViewStack CreateViewStack (
89106
return result;
90107
}
91108

92-
private AddinListView CreateAddinList (SystemManager system, IChromeService chrome)
109+
private AddinListView CreateAddinList ()
93110
{
94-
AddinListView result = new (system, chrome);
111+
AddinListView result = AddinListView.New ();
95112
result.OnAddinChanged += (_, _) => LoadAll ();
96113
return result;
97114
}
@@ -262,7 +279,7 @@ private async void HandleInstallFromFileClicked ()
262279
.Where (f => !string.IsNullOrEmpty (f))
263280
.ToArray ();
264281

265-
InstallDialog install_dialog = new (this, setup_service); // TODO: dispose properly after making async
282+
InstallDialog install_dialog = InstallDialog.New (this, setup_service); // TODO: dispose properly after making async
266283
if (install_dialog.InitForInstall (files)) {
267284
install_dialog.OnSuccess += (_, _) => LoadAll ();
268285
install_dialog.Show ();

0 commit comments

Comments
 (0)