11using System ;
22using System . Collections . Generic ;
33using System . Collections . ObjectModel ;
4+ using System . Diagnostics . CodeAnalysis ;
45using System . Linq ;
56
67namespace 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}
0 commit comments