Skip to content

Commit 40c2841

Browse files
committed
Port ToolBarDropDownButton to the new subclass init style
1 parent d56daac commit 40c2841

9 files changed

Lines changed: 61 additions & 51 deletions

File tree

Pinta.Core/Classes/BaseTool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ protected bool IsActiveTool ()
336336
protected ToolBarDropDownButton AlphaBlendingDropDown {
337337
get {
338338
if (alphablending_button is null) {
339-
alphablending_button = new ToolBarDropDownButton ();
339+
alphablending_button = ToolBarDropDownButton.New ();
340340

341341
alphablending_button.AddItem (Translations.GetString ("Normal Blending"), Pinta.Resources.Icons.BlendingNormal, true);
342342
alphablending_button.AddItem (Translations.GetString ("Overwrite"), Pinta.Resources.Icons.BlendingOverwrite, false);
@@ -353,7 +353,7 @@ protected ToolBarDropDownButton AlphaBlendingDropDown {
353353
private ToolBarDropDownButton AntialiasingDropDown {
354354
get {
355355
if (antialiasing_button is null) {
356-
antialiasing_button = new ToolBarDropDownButton ();
356+
antialiasing_button = ToolBarDropDownButton.New ();
357357

358358
antialiasing_button.AddItem (Translations.GetString ("Antialiasing On"), Pinta.Resources.Icons.AntiAliasingEnabled, true);
359359
antialiasing_button.AddItem (Translations.GetString ("Antialiasing Off"), Pinta.Resources.Icons.AntiAliasingDisabled, false);

Pinta.Core/Widgets/ToolBarDropDownButton.cs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56

67
namespace Pinta.Core;
78

8-
public sealed class ToolBarDropDownButton : Gtk.DropDown
9+
[GObject.Subclass<Gtk.DropDown>]
10+
public sealed partial class ToolBarDropDownButton
911
{
10-
private readonly bool show_label;
12+
private bool show_label = false;
1113

12-
private Gtk.Box selected_box;
13-
private Gtk.Image dropdown_icon;
14-
private Gtk.Label dropdown_label;
14+
private readonly Gtk.Box selected_box = Gtk.Box.New (Gtk.Orientation.Horizontal, 0);
15+
private readonly Gtk.Image dropdown_icon = Gtk.Image.New ();
16+
private readonly Gtk.Label dropdown_label = Gtk.Label.New (null);
1517

1618
// We store the index of the previous selection to avoid having to iterate through all items on the list.
1719
private int previous_index = 0;
18-
private Gtk.StringList string_list;
20+
private readonly Gtk.StringList string_list = Gtk.StringList.New (null);
1921

20-
private readonly List<ToolBarItem> items;
21-
private readonly List<ToolBarItemWidget> toolbar_item_widgets;
22-
public ReadOnlyCollection<ToolBarItem> Items { get; }
22+
private readonly List<ToolBarItem> items = [];
23+
private readonly List<ToolBarItemWidget> toolbar_item_widgets = [];
24+
public ReadOnlyCollection<ToolBarItem> Items { get; private set; }
2325

24-
public ToolBarDropDownButton (bool showLabel = false)
26+
[MemberNotNull (nameof (Items))]
27+
partial void Initialize ()
2528
{
2629
// We create the widgets inside the dropdown to avoid having to create yet another custom widget
2730
// for the selectedFactory. Also, we can reference them directly when updated, avoiding
2831
// .nextSibling hacks.
29-
selected_box = Gtk.Box.New (Gtk.Orientation.Horizontal, 0);
30-
dropdown_icon = Gtk.Image.New ();
31-
dropdown_label = Gtk.Label.New (str: null);
3232
selected_box.Append (dropdown_icon);
3333
selected_box.Append (dropdown_label);
3434

35-
items = [];
3635
Items = new (items);
37-
toolbar_item_widgets = [];
38-
show_label = showLabel;
3936

40-
string_list = Gtk.StringList.New (strings: null);
4137
SetModel (string_list);
4238

4339
Gtk.SignalListItemFactory selectedFactory = Gtk.SignalListItemFactory.New ();
@@ -50,6 +46,13 @@ public ToolBarDropDownButton (bool showLabel = false)
5046
SetListFactory (listFactory);
5147
}
5248

49+
public static ToolBarDropDownButton New (bool showLabel = false)
50+
{
51+
ToolBarDropDownButton button = NewWithProperties ([]);
52+
button.show_label = showLabel;
53+
return button;
54+
}
55+
5356
private void OnSetupSelectedItem (Gtk.SignalListItemFactory factory, Gtk.SignalListItemFactory.SetupSignalArgs args)
5457
{
5558
Gtk.ListItem item = (Gtk.ListItem) args.Object;
@@ -84,7 +87,7 @@ public ToolBarItem AddItem (string text, string imageId)
8487

8588
public ToolBarItem AddItem (string text, string imageId, object? tag)
8689
{
87-
ToolBarItemWidget widget = new (text, imageId);
90+
ToolBarItemWidget widget = ToolBarItemWidget.New (text, imageId);
8891
toolbar_item_widgets.Add (widget);
8992
// We append an empty string because we only need the list's index.
9093
// Otherwise, we'd need to make ToolBarItem inherit from GObject, which is undesired.
@@ -154,28 +157,35 @@ public T GetTagOrDefault<T> (T defaultValue)
154157
=> Tag is T value ? value : defaultValue;
155158
}
156159

157-
public sealed class ToolBarItemWidget : Gtk.Box
160+
[GObject.Subclass<Gtk.Box>]
161+
internal sealed partial class ToolBarItemWidget
158162
{
159-
public ToolBarItemWidget (string text, string imageId)
160-
{
161-
Gtk.Image image = Gtk.Image.NewFromIconName (imageId);
162-
Gtk.Label label = Gtk.Label.New (text);
163+
private readonly Gtk.Image image = Gtk.Image.New ();
164+
private readonly Gtk.Label label = Gtk.Label.New (null);
165+
private readonly Gtk.Image selected_icon = Gtk.Image.NewFromIconName (Resources.StandardIcons.ObjectSelect);
163166

164-
Append (image);
165-
Append (label);
166-
167-
selected_icon = Gtk.Image.NewFromIconName (Resources.StandardIcons.ObjectSelect);
167+
partial void Initialize ()
168+
{
168169
selected_icon.Visible = false;
169170
selected_icon.Hexpand = true;
170171
selected_icon.Halign = Gtk.Align.End;
171172

173+
Append (image);
174+
Append (label);
172175
Append (selected_icon);
173176
}
174177

178+
public static ToolBarItemWidget New (string text, string imageId)
179+
{
180+
ToolBarItemWidget widget = NewWithProperties ([]);
181+
widget.image.IconName = imageId;
182+
widget.label.SetLabel (text);
183+
184+
return widget;
185+
}
186+
175187
public void SetCheckmarkVisible (bool visible)
176188
{
177189
selected_icon.Visible = visible;
178190
}
179-
180-
private Gtk.Image selected_icon;
181191
}

Pinta.Tools/Editable/EditEngines/BaseEditEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public void HandleBuildToolBar (Gtk.Box tb, ISettingsService settings, string to
269269
tb.Append (shape_type_label);
270270

271271
if (shape_type_button == null) {
272-
shape_type_button = new ToolBarDropDownButton ();
272+
shape_type_button = ToolBarDropDownButton.New ();
273273

274274
shape_type_button.AddItem (Translations.GetString ("Open Line/Curve Series"), Resources.Icons.ToolLine, 0);
275275
shape_type_button.AddItem (Translations.GetString ("Closed Line/Curve Series"), Resources.Icons.ToolRectangle, 1);
@@ -331,7 +331,7 @@ protected virtual void BuildShapeToolBar (Gtk.Box tb, ISettingsService settings,
331331
tb.Append (fill_label);
332332

333333
if (fill_button == null) {
334-
fill_button = new ToolBarDropDownButton ();
334+
fill_button = ToolBarDropDownButton.New ();
335335

336336
fill_button.AddItem (Translations.GetString ("Outline Shape"), Resources.Icons.FillStyleOutline, 0);
337337
fill_button.AddItem (Translations.GetString ("Fill Shape"), Resources.Icons.FillStyleFill, 1);

Pinta.Tools/Tools/ColorPickerTool.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private ColorBgra GetPixel (Document document, PointI position)
211211
private ToolBarDropDownButton ToolSelectionDropDown {
212212
get {
213213
if (tool_select is null) {
214-
tool_select = new ToolBarDropDownButton (true);
214+
tool_select = ToolBarDropDownButton.New (true);
215215

216216
tool_select.AddItem (Translations.GetString ("Do not switch tool"), Pinta.Resources.Icons.ToolColorPicker, 0);
217217
tool_select.AddItem (Translations.GetString ("Switch to previous tool"), Pinta.Resources.Icons.ToolColorPickerPreviousTool, 1);
@@ -227,7 +227,7 @@ private ToolBarDropDownButton ToolSelectionDropDown {
227227
private ToolBarDropDownButton SampleSizeDropDown {
228228
get {
229229
if (sample_size is null) {
230-
sample_size = new ToolBarDropDownButton (true);
230+
sample_size = ToolBarDropDownButton.New (true);
231231

232232
// Change the cursor when the SampleSize is changed.
233233
sample_size.SelectedItemChanged += (sender, e) => SetCursor (DefaultCursor);
@@ -248,7 +248,7 @@ private ToolBarDropDownButton SampleSizeDropDown {
248248
private ToolBarDropDownButton SampleTypeDropDown {
249249
get {
250250
if (sample_type is null) {
251-
sample_type = new ToolBarDropDownButton (true);
251+
sample_type = ToolBarDropDownButton.New (true);
252252

253253
sample_type.AddItem (Translations.GetString ("Layer"), Pinta.Resources.Icons.LayerMergeDown, true);
254254
sample_type.AddItem (Translations.GetString ("Image"), Pinta.Resources.Icons.ResizeCanvasBase, false);

Pinta.Tools/Tools/FloodTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected virtual void OnFillRegionComputed (Document document, BitMask stencil)
126126
protected ToolBarDropDownButton ModeDropDown {
127127
get {
128128
if (mode_button is null) {
129-
mode_button = new ToolBarDropDownButton ();
129+
mode_button = ToolBarDropDownButton.New ();
130130

131131
mode_button.AddItem (Translations.GetString ("Contiguous"), Pinta.Resources.Icons.ToolFreeformShape, false);
132132
mode_button.AddItem (Translations.GetString ("Global"), Pinta.Resources.Icons.HelpWebsite, true);

Pinta.Tools/Tools/FreeformShapeTool.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
//
1+
//
22
// FreeformShapeTool.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
@@ -220,7 +220,7 @@ protected override void OnSaveSettings (ISettingsService settings)
220220
private ToolBarDropDownButton FillDropDown {
221221
get {
222222
if (fill_button == null) {
223-
fill_button = new ToolBarDropDownButton ();
223+
fill_button = ToolBarDropDownButton.New ();
224224

225225
fill_button.AddItem (Translations.GetString ("Outline Shape"), Pinta.Resources.Icons.FillStyleOutline, 0);
226226
fill_button.AddItem (Translations.GetString ("Fill Shape"), Pinta.Resources.Icons.FillStyleFill, 1);

Pinta.Tools/Tools/GradientTool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public GradientData Data {
289289
private ToolBarDropDownButton GradientDropDown {
290290
get {
291291
if (gradient_button == null) {
292-
gradient_button = new ToolBarDropDownButton ();
292+
gradient_button = ToolBarDropDownButton.New ();
293293

294294
gradient_button.AddItem (Translations.GetString ("Linear Gradient"), Pinta.Resources.Icons.GradientLinear, GradientType.Linear);
295295
gradient_button.AddItem (Translations.GetString ("Linear Reflected Gradient"), Pinta.Resources.Icons.GradientLinearReflected, GradientType.LinearReflected);
@@ -323,7 +323,7 @@ void HandlePintaCorePalettePrimaryColorChanged (object? sender, EventArgs e)
323323
private ToolBarDropDownButton ColorModeDropDown {
324324
get {
325325
if (color_mode_button == null) {
326-
color_mode_button = new ToolBarDropDownButton ();
326+
color_mode_button = ToolBarDropDownButton.New ();
327327

328328
color_mode_button.AddItem (Translations.GetString ("Color Mode"), Pinta.Resources.Icons.ColorModeColor, GradientColorMode.Color);
329329
color_mode_button.AddItem (Translations.GetString ("Transparency Mode"), Pinta.Resources.Icons.ColorModeTransparency, GradientColorMode.Transparency);

Pinta.Tools/Tools/LassoSelectTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected override void OnSaveSettings (ISettingsService settings)
215215
private ToolBarDropDownButton LassoModeButtom {
216216
get {
217217
if (lasso_mode_buttom is null) {
218-
lasso_mode_buttom = new ToolBarDropDownButton ();
218+
lasso_mode_buttom = ToolBarDropDownButton.New ();
219219

220220
lasso_mode_buttom.AddItem (Translations.GetString ("Freeform"), Pinta.Resources.Icons.LassoFreeform, false);
221221
lasso_mode_buttom.AddItem (Translations.GetString ("Polygon"), Pinta.Resources.Icons.LassoPolygon, true);

Pinta.Tools/Tools/TextTool.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ protected override void OnBuildToolBar (Gtk.Box tb)
175175
tb.Append (GtkExtensions.CreateToolBarSeparator ());
176176

177177
if (variant_btn == null) {
178-
variant_btn = new ToolBarDropDownButton ();
178+
variant_btn = ToolBarDropDownButton.New ();
179179

180180
variant_btn.AddItem (
181181
// Translators: 'Normal' refers to the font-variant text property
@@ -247,7 +247,7 @@ protected override void OnBuildToolBar (Gtk.Box tb)
247247
tb.Append (GtkExtensions.CreateToolBarSeparator ());
248248

249249
if (weight_btn == null) {
250-
weight_btn = new ToolBarDropDownButton ();
250+
weight_btn = ToolBarDropDownButton.New ();
251251

252252
weight_btn.AddItem (
253253
// Translators: 'Thin' (100) refers to the font-weight text property
@@ -399,7 +399,7 @@ protected override void OnBuildToolBar (Gtk.Box tb)
399399
tb.Append (fill_label);
400400

401401
if (fill_button == null) {
402-
fill_button = new ToolBarDropDownButton ();
402+
fill_button = ToolBarDropDownButton.New ();
403403

404404
fill_button.AddItem (Translations.GetString ("Normal"), Pinta.Resources.Icons.FillStyleFill, 0);
405405
fill_button.AddItem (Translations.GetString ("Normal and Outline"), Pinta.Resources.Icons.FillStyleOutlineFill, 1);
@@ -439,7 +439,7 @@ protected override void OnBuildToolBar (Gtk.Box tb)
439439
tb.Append (join_sep);
440440

441441
if (join_btn == null) {
442-
join_btn = new ToolBarDropDownButton ();
442+
join_btn = ToolBarDropDownButton.New ();
443443

444444
join_btn.AddItem (
445445
// Translators: 'Miter Join' refers to the Cairo.LineJoin property

0 commit comments

Comments
 (0)