Skip to content

Commit 440335f

Browse files
committed
Port several more dialogs to the new subclass init style
1 parent 1afbd31 commit 440335f

7 files changed

Lines changed: 121 additions & 77 deletions

File tree

Pinta.Gui.Widgets/Widgets/ToolBoxWidget.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,35 @@
44

55
namespace Pinta.Gui.Widgets;
66

7-
public sealed class ToolBoxWidget : Gtk.FlowBox
7+
[GObject.Subclass<Gtk.FlowBox>]
8+
public sealed partial class ToolBoxWidget
89
{
9-
private readonly ToolManager tools;
10-
// Stores the button corresponding to each tool.
10+
private ToolManager tools = null!; // NRT - set in factory method
11+
// Stores the button corresponding to each tool.
1112
private readonly Dictionary<BaseTool, Gtk.ToggleButton> tool_buttons = new ();
1213
// Dummy ToggleButton to use for grouping together the tools' buttons.
1314
private readonly Gtk.ToggleButton toggle_group = Gtk.ToggleButton.New ();
1415

15-
public ToolBoxWidget (ToolManager tools)
16+
partial void Initialize ()
1617
{
17-
tools.ToolAdded += (_, e) => HandleToolAdded (e.Tool);
18-
tools.ToolRemoved += (_, e) => HandleToolRemoved (e.Tool);
19-
tools.ToolActivated += (_, e) => HandleToolActivated (e.Tool);
20-
21-
// --- Initialization (Gtk.FlowBox)
22-
2318
SetOrientation (Gtk.Orientation.Vertical);
2419
MinChildrenPerLine = 8; // Pinta 3 has 22 default tools, meaning a max of 3 columns regardless of size, smaller values don't lead to better use of visual space.
2520
MaxChildrenPerLine = 1024; // Allow for single column if there's sufficient space to do so.
2621
SelectionMode = Gtk.SelectionMode.None; // Don't allow the buttons to be selected.
22+
}
2723

28-
// --- References to keep
24+
public static ToolBoxWidget New (ToolManager tools)
25+
{
26+
ToolBoxWidget widget = NewWithProperties ([]);
27+
widget.Configure (tools);
28+
return widget;
29+
}
30+
31+
private void Configure (ToolManager tools)
32+
{
33+
tools.ToolAdded += (_, e) => HandleToolAdded (e.Tool);
34+
tools.ToolRemoved += (_, e) => HandleToolRemoved (e.Tool);
35+
tools.ToolActivated += (_, e) => HandleToolActivated (e.Tool);
2936

3037
this.tools = tools;
3138
}

Pinta/Actions/File/ModifyCompressionAction.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
//
1+
//
22
// ModifyCompressionAction.cs
3-
//
3+
//
44
// Author:
55
// Jonathan Pobst <monkey@jpobst.com>
6-
//
6+
//
77
// Copyright (c) 2010 Jonathan Pobst
8-
//
8+
//
99
// Permission is hereby granted, free of charge, to any person obtaining a copy
1010
// of this software and associated documentation files (the "Software"), to deal
1111
// in the Software without restriction, including without limitation the rights
1212
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1313
// copies of the Software, and to permit persons to whom the Software is
1414
// furnished to do so, subject to the following conditions:
15-
//
15+
//
1616
// The above copyright notice and this permission notice shall be included in
1717
// all copies or substantial portions of the Software.
18-
//
18+
//
1919
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2020
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2121
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -48,7 +48,7 @@ void IActionHandler.Uninitialize ()
4848

4949
private void Activated (object? sender, ModifyCompressionEventArgs e)
5050
{
51-
JpegCompressionDialog dlg = new (e.Quality, e.ParentWindow);
51+
JpegCompressionDialog dlg = JpegCompressionDialog.New (e.Quality, e.ParentWindow);
5252

5353
if (dlg.RunBlocking () == Gtk.ResponseType.Ok)
5454
e.Quality = dlg.CompressionLevel;

Pinta/Actions/Layers/LayerPropertiesAction.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
//
1+
//
22
// LayerPropertiesAction.cs
3-
//
3+
//
44
// Author:
55
// Jonathan Pobst <monkey@jpobst.com>
6-
//
6+
//
77
// Copyright (c) 2010 Jonathan Pobst
8-
//
8+
//
99
// Permission is hereby granted, free of charge, to any person obtaining a copy
1010
// of this software and associated documentation files (the "Software"), to deal
1111
// in the Software without restriction, including without limitation the rights
1212
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1313
// copies of the Software, and to permit persons to whom the Software is
1414
// furnished to do so, subject to the following conditions:
15-
//
15+
//
1616
// The above copyright notice and this permission notice shall be included in
1717
// all copies or substantial portions of the Software.
18-
//
18+
//
1919
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2020
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2121
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -57,7 +57,7 @@ void IActionHandler.Uninitialize ()
5757
private async void Activated (object sender, EventArgs e)
5858
{
5959
Document active = workspace.ActiveDocument;
60-
using LayerPropertiesDialog dialog = new (chrome, workspace);
60+
using LayerPropertiesDialog dialog = LayerPropertiesDialog.New (chrome, workspace);
6161
try {
6262
Gtk.ResponseType response = await dialog.RunAsync ();
6363

Pinta/Dialogs/JpegCompressionDialog.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
//
1+
//
22
// JpegCompressionDialog.cs
3-
//
3+
//
44
// Author:
55
// Maia Kozheva <sikon@ubuntu.com>
6-
//
6+
//
77
// Copyright (c) 2010 Maia Kozheva
8-
//
8+
//
99
// Permission is hereby granted, free of charge, to any person obtaining a copy
1010
// of this software and associated documentation files (the "Software"), to deal
1111
// in the Software without restriction, including without limitation the rights
1212
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1313
// copies of the Software, and to permit persons to whom the Software is
1414
// furnished to do so, subject to the following conditions:
15-
//
15+
//
1616
// The above copyright notice and this permission notice shall be included in
1717
// all copies or substantial portions of the Software.
18-
//
18+
//
1919
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2020
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2121
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -24,27 +24,28 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
// THE SOFTWARE.
2626

27+
using System.Diagnostics.CodeAnalysis;
2728
using Pinta.Core;
2829

2930
namespace Pinta;
3031

31-
public sealed class JpegCompressionDialog : Gtk.Dialog
32+
[GObject.Subclass<Gtk.Dialog>]
33+
public sealed partial class JpegCompressionDialog
3234
{
33-
private readonly Gtk.Scale compression_level;
35+
private Gtk.Scale compression_level;
3436

35-
public JpegCompressionDialog (int defaultQuality, Gtk.Window parent)
37+
[MemberNotNull (nameof (compression_level))]
38+
partial void Initialize ()
3639
{
3740
Gtk.Label qualityLabel = Gtk.Label.New (Translations.GetString ("Quality: "));
3841
qualityLabel.Xalign = 0;
3942

4043
Gtk.Scale levelScale = Gtk.Scale.NewWithRange (Gtk.Orientation.Horizontal, 1, 100, 1);
41-
levelScale.SetValue (defaultQuality);
4244
levelScale.DrawValue = true;
4345

4446
// --- Initialization (Gtk.Window)
4547

4648
Title = Translations.GetString ("JPEG Quality");
47-
TransientFor = parent;
4849
Modal = true;
4950

5051
// --- Initialization (Gtk.Dialog)
@@ -64,6 +65,14 @@ public JpegCompressionDialog (int defaultQuality, Gtk.Window parent)
6465
compression_level = levelScale;
6566
}
6667

68+
public static JpegCompressionDialog New (int defaultQuality, Gtk.Window parent)
69+
{
70+
JpegCompressionDialog dialog = NewWithProperties ([]);
71+
dialog.compression_level.SetValue (defaultQuality);
72+
dialog.TransientFor = parent;
73+
return dialog;
74+
}
75+
6776
public int CompressionLevel
6877
=> (int) compression_level.GetValue ();
6978
}

Pinta/Dialogs/LayerPropertiesDialog.cs

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,72 +26,60 @@
2626

2727
using System;
2828
using System.Collections.Immutable;
29+
using System.Diagnostics.CodeAnalysis;
2930
using Pinta.Core;
3031

3132
namespace Pinta;
3233

33-
public sealed class LayerPropertiesDialog : Gtk.Dialog
34+
[GObject.Subclass<Gtk.Dialog>]
35+
public sealed partial class LayerPropertiesDialog
3436
{
35-
private readonly LayerProperties initial_properties;
37+
private LayerProperties initial_properties = new (string.Empty, false, 0.0, BlendMode.Normal);
3638

3739
private double current_layer_opacity;
3840
private bool current_layer_hidden;
39-
private string current_layer_name;
41+
private string current_layer_name = string.Empty;
4042
private BlendMode current_layer_blend_mode;
4143

42-
private readonly Gtk.Entry layer_name_entry;
43-
private readonly Gtk.CheckButton visibility_checkbox;
44-
private readonly Gtk.SpinButton opacity_spinner;
45-
private readonly Gtk.Scale opacity_slider;
46-
private readonly Gtk.ComboBoxText blend_combo_box;
44+
private Gtk.Entry layer_name_entry;
45+
private Gtk.CheckButton visibility_checkbox;
46+
private Gtk.SpinButton opacity_spinner;
47+
private Gtk.Scale opacity_slider;
48+
private Gtk.ComboBoxText blend_combo_box;
4749

48-
private readonly WorkspaceManager workspace;
50+
private WorkspaceManager workspace = null!; // NRT - set by factory method
4951

50-
public LayerPropertiesDialog (ChromeManager chrome, WorkspaceManager workspace)
52+
[MemberNotNull (nameof (layer_name_entry))]
53+
[MemberNotNull (nameof (visibility_checkbox))]
54+
[MemberNotNull (nameof (opacity_slider))]
55+
[MemberNotNull (nameof (opacity_spinner))]
56+
[MemberNotNull (nameof (blend_combo_box))]
57+
partial void Initialize ()
5158
{
5259
const int spacing = 6;
5360

54-
Document doc = workspace.ActiveDocument;
55-
56-
string currentLayerName = doc.Layers.CurrentUserLayer.Name;
57-
bool currentLayerHidden = doc.Layers.CurrentUserLayer.Hidden;
58-
double currentLayerOpacity = doc.Layers.CurrentUserLayer.Opacity;
59-
BlendMode currentLayerBlendMode = doc.Layers.CurrentUserLayer.BlendMode;
60-
61-
LayerProperties initialProperties = new (
62-
currentLayerName,
63-
currentLayerHidden,
64-
currentLayerOpacity,
65-
currentLayerBlendMode);
66-
6761
Gtk.Label nameLabel = Gtk.Label.New (Translations.GetString ("Name:"));
6862
nameLabel.Halign = Gtk.Align.End;
6963

7064
Gtk.Entry layerNameEntry = Gtk.Entry.New ();
7165
layerNameEntry.Hexpand = true;
7266
layerNameEntry.Halign = Gtk.Align.Fill;
73-
layerNameEntry.SetText (initialProperties.Name);
7467
layerNameEntry.OnChanged += OnLayerNameChanged;
7568
layerNameEntry.SetActivatesDefault (true);
7669

7770
Gtk.CheckButton visibilityCheckbox = Gtk.CheckButton.NewWithLabel (Translations.GetString ("Visible"));
78-
visibilityCheckbox.Active = !initialProperties.Hidden;
7971
visibilityCheckbox.OnToggled += OnVisibilityToggled;
8072

8173
Gtk.Label blendLabel = Gtk.Label.New (Translations.GetString ("Blend Mode") + ":");
8274
blendLabel.Halign = Gtk.Align.End;
8375

84-
var allBlendmodes = UserBlendOps.GetAllBlendModeNames ().ToImmutableArray ();
85-
var index = allBlendmodes.IndexOf (UserBlendOps.GetBlendModeName (currentLayerBlendMode));
86-
8776
Gtk.ComboBoxText blendComboBox = Gtk.ComboBoxText.New ();
8877

8978
foreach (string name in UserBlendOps.GetAllBlendModeNames ())
9079
blendComboBox.AppendText (name);
9180

9281
blendComboBox.Hexpand = true;
9382
blendComboBox.Halign = Gtk.Align.Fill;
94-
blendComboBox.Active = index;
9583
blendComboBox.OnChanged += OnBlendModeChanged;
9684

9785
Gtk.Label opacityLabel = Gtk.Label.New (Translations.GetString ("Opacity:"));
@@ -100,7 +88,6 @@ public LayerPropertiesDialog (ChromeManager chrome, WorkspaceManager workspace)
10088
Gtk.SpinButton opacitySpinner = Gtk.SpinButton.NewWithRange (0, 100, 1);
10189
opacitySpinner.Adjustment!.PageIncrement = 10;
10290
opacitySpinner.ClimbRate = 1;
103-
opacitySpinner.Value = Math.Round (initialProperties.Opacity * 100);
10491
opacitySpinner.OnValueChanged += OnOpacitySpinnerChanged;
10592
opacitySpinner.SetActivatesDefaultImmediate (true);
10693

@@ -109,7 +96,6 @@ public LayerPropertiesDialog (ChromeManager chrome, WorkspaceManager workspace)
10996
opacitySlider.Adjustment!.PageIncrement = 10;
11097
opacitySlider.Hexpand = true;
11198
opacitySlider.Halign = Gtk.Align.Fill;
112-
opacitySlider.SetValue (Math.Round (initialProperties.Opacity * 100));
11399
opacitySlider.OnValueChanged += OnOpacitySliderChanged;
114100

115101
Gtk.Box opacityBox = Gtk.Box.New (Gtk.Orientation.Horizontal, spacing);
@@ -131,7 +117,6 @@ public LayerPropertiesDialog (ChromeManager chrome, WorkspaceManager workspace)
131117
// --- Initialization (Gtk.Window)
132118

133119
Title = Translations.GetString ("Layer Properties");
134-
TransientFor = chrome.MainWindow;
135120
Modal = true;
136121
DefaultWidth = 349;
137122
DefaultHeight = 224;
@@ -156,15 +141,48 @@ public LayerPropertiesDialog (ChromeManager chrome, WorkspaceManager workspace)
156141
blend_combo_box = blendComboBox;
157142
opacity_spinner = opacitySpinner;
158143
opacity_slider = opacitySlider;
144+
}
145+
146+
public static LayerPropertiesDialog New (IChromeService chrome, WorkspaceManager workspace)
147+
{
148+
LayerPropertiesDialog dialog = NewWithProperties ([]);
149+
dialog.Configure (chrome, workspace);
150+
return dialog;
151+
}
152+
153+
private void Configure (IChromeService chrome, WorkspaceManager workspace)
154+
{
155+
this.workspace = workspace;
156+
TransientFor = chrome.MainWindow;
157+
158+
Document doc = workspace.ActiveDocument;
159+
160+
string currentLayerName = doc.Layers.CurrentUserLayer.Name;
161+
bool currentLayerHidden = doc.Layers.CurrentUserLayer.Hidden;
162+
double currentLayerOpacity = doc.Layers.CurrentUserLayer.Opacity;
163+
BlendMode currentLayerBlendMode = doc.Layers.CurrentUserLayer.BlendMode;
164+
165+
LayerProperties initialProperties = new (
166+
currentLayerName,
167+
currentLayerHidden,
168+
currentLayerOpacity,
169+
currentLayerBlendMode);
170+
171+
layer_name_entry.SetText (initialProperties.Name);
172+
visibility_checkbox.Active = !initialProperties.Hidden;
173+
opacity_spinner.Value = Math.Round (initialProperties.Opacity * 100);
174+
opacity_slider.SetValue (Math.Round (initialProperties.Opacity * 100));
175+
176+
var allBlendmodes = UserBlendOps.GetAllBlendModeNames ().ToImmutableArray ();
177+
var index = allBlendmodes.IndexOf (UserBlendOps.GetBlendModeName (currentLayerBlendMode));
178+
blend_combo_box.Active = index;
159179

160180
current_layer_name = currentLayerName;
161181
current_layer_hidden = currentLayerHidden;
162182
current_layer_opacity = currentLayerOpacity;
163183
current_layer_blend_mode = currentLayerBlendMode;
164184

165185
initial_properties = initialProperties;
166-
167-
this.workspace = workspace;
168186
}
169187

170188
public bool AreLayerPropertiesUpdated =>

0 commit comments

Comments
 (0)