Skip to content

Commit d91e4eb

Browse files
authored
FormatBar: clean up widget construction (#1772)
* FormatBar: move lang popover construction to construct * FormatBar: move tab width popover construction to construct * FormatBar: move goto line popover construction to construct
1 parent b09bd8e commit d91e4eb

1 file changed

Lines changed: 135 additions & 145 deletions

File tree

src/Widgets/FormatBar.vala

Lines changed: 135 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@
2020
public class Code.FormatBar : Gtk.Box {
2121
public bool tab_style_set_by_editor_config { get; set; default = false; }
2222
public bool tab_width_set_by_editor_config { get; set; default = false; }
23-
public Gtk.InfoBar editorconfig_infobar { get; set construct; }
24-
public Gtk.Box tab_box { get; set construct; }
25-
public Gtk.SpinButton width_spinbutton { get; set construct; }
2623

2724
private FormatButton line_menubutton;
2825
private FormatButton lang_menubutton;
2926
private FormatButton tab_menubutton;
3027
private Granite.SwitchModelButton space_tab_modelbutton;
3128
private Gtk.Entry goto_entry;
29+
private Gtk.InfoBar editorconfig_infobar;
3230
private Gtk.ListBox lang_selection_listbox;
33-
private Gtk.SearchEntry lang_selection_filter;
3431
private Gtk.SourceLanguageManager manager;
32+
private Gtk.SpinButton width_spinbutton;
3533
private LangEntry normal_entry;
3634

3735
private unowned Scratch.Services.Document? doc = null;
@@ -41,159 +39,155 @@ public class Code.FormatBar : Gtk.Box {
4139

4240
manager = Gtk.SourceLanguageManager.get_default ();
4341

44-
tab_menubutton = new FormatButton () {
45-
icon = new ThemedIcon ("format-indent-more-symbolic")
42+
editorconfig_infobar = new Gtk.InfoBar () {
43+
margin_top = 9,
44+
margin_end = 9,
45+
margin_start = 9
4646
};
47+
editorconfig_infobar.get_content_area ().add (new Gtk.Label (_("Some settings set by EditorConfig file")));
48+
editorconfig_infobar.get_style_context ().add_class (Gtk.STYLE_CLASS_FRAME);
4749

48-
lang_menubutton = new FormatButton () {
49-
icon = new ThemedIcon ("application-x-class-file-symbolic"),
50-
tooltip_text = _("Document language")
51-
};
50+
var autoindent_modelbutton = new Granite.SwitchModelButton (_("Automatic Indentation"));
5251

53-
line_menubutton = new FormatButton () {
54-
icon = new ThemedIcon ("view-continuous-symbolic")
52+
space_tab_modelbutton = new Granite.SwitchModelButton (_("Insert Spaces Instead Of Tabs"));
53+
54+
width_spinbutton = new Gtk.SpinButton.with_range (2, 16, 1);
55+
56+
var width_label = new Gtk.Label (_("Tab width")) {
57+
halign = START,
58+
hexpand = true,
59+
mnemonic_widget = width_spinbutton
5560
};
56-
line_menubutton.tooltip_markup = Granite.markup_accel_tooltip (
57-
((Scratch.Application) GLib.Application.get_default ()).get_accels_for_action (
58-
Scratch.MainWindow.ACTION_PREFIX + Scratch.MainWindow.ACTION_GO_TO
59-
),
60-
_("Line number")
61-
);
6261

63-
homogeneous = true;
64-
add (tab_menubutton);
65-
add (lang_menubutton);
66-
add (line_menubutton);
62+
var tab_box = new Gtk.Box (HORIZONTAL, 12) {
63+
margin_top = 6,
64+
margin_end = 12,
65+
margin_start = 12,
66+
};
67+
tab_box.add (width_label);
68+
tab_box.add (width_spinbutton);
6769

68-
create_tabulation_popover ();
69-
create_language_popover ();
70-
create_line_popover ();
71-
}
70+
var box = new Gtk.Box (VERTICAL, 0) {
71+
margin_bottom = 12
72+
};
73+
box.add (editorconfig_infobar);
74+
box.add (autoindent_modelbutton);
75+
box.add (space_tab_modelbutton);
76+
box.add (tab_box);
77+
box.show_all ();
7278

73-
public void activate_line_menubutton () {
74-
line_menubutton.active = true;
75-
}
79+
var tab_popover = new Gtk.Popover (null) {
80+
position = BOTTOM,
81+
child = box
82+
};
7683

77-
private void create_language_popover () {
78-
lang_selection_listbox = new Gtk.ListBox ();
79-
lang_selection_listbox.selection_mode = Gtk.SelectionMode.SINGLE;
80-
lang_selection_listbox.set_sort_func ((row1, row2) => {
81-
return ((LangEntry) row1).lang_name.collate (((LangEntry) row2).lang_name);
82-
});
83-
lang_selection_listbox.set_filter_func ((row) => {
84-
//Both are lowercased so that the case doesn't matter when comparing.
85-
return (((LangEntry) row).lang_name.down ().contains (lang_selection_filter.text.down ().strip ()));
86-
});
84+
tab_menubutton = new FormatButton () {
85+
icon = new ThemedIcon ("format-indent-more-symbolic"),
86+
popover = tab_popover
87+
};
8788

88-
lang_selection_filter = new Gtk.SearchEntry () {
89+
var lang_selection_filter = new Gtk.SearchEntry () {
8990
margin_top = 12,
9091
margin_bottom = 6,
9192
margin_start = 12,
9293
margin_end = 12,
9394
placeholder_text = _("Filter languages")
9495
};
9596

96-
lang_selection_filter.changed.connect (() => {
97-
lang_selection_listbox.invalidate_filter ();
98-
});
99-
100-
var lang_scrolled = new Gtk.ScrolledWindow (null, null) {
101-
hscrollbar_policy = Gtk.PolicyType.NEVER,
102-
height_request = 350,
103-
hexpand = true,
104-
vexpand = true,
105-
margin_top = 3,
106-
margin_bottom = 3,
107-
child = lang_selection_listbox
97+
lang_selection_listbox = new Gtk.ListBox () {
98+
selection_mode = SINGLE
10899
};
100+
lang_selection_listbox.set_sort_func ((row1, row2) => {
101+
return ((LangEntry) row1).lang_name.collate (((LangEntry) row2).lang_name);
102+
});
103+
lang_selection_listbox.set_filter_func ((row) => {
104+
//Both are lowercased so that the case doesn't matter when comparing.
105+
return (((LangEntry) row).lang_name.down ().contains (lang_selection_filter.text.down ().strip ()));
106+
});
109107

110-
unowned string[]? ids = manager.get_language_ids ();
111108
unowned SList<Gtk.RadioButton> group = null;
112-
foreach (unowned string id in ids) {
109+
foreach (unowned string id in manager.get_language_ids ()) {
113110
weak Gtk.SourceLanguage lang = manager.get_language (id);
114111
var entry = new LangEntry (id, lang.name, group);
115112
group = entry.get_radio_group ();
116113
lang_selection_listbox.add (entry);
117114
}
118115

119116
normal_entry = new LangEntry (null, _("Plain Text"), group);
117+
120118
lang_selection_listbox.add (normal_entry);
121119

122-
var popover_content = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
120+
var lang_scrolled = new Gtk.ScrolledWindow (null, null) {
121+
child = lang_selection_listbox,
122+
hscrollbar_policy = NEVER,
123+
height_request = 350,
124+
hexpand = true,
125+
vexpand = true,
126+
margin_top = 3,
127+
margin_bottom = 3
128+
};
129+
130+
var popover_content = new Gtk.Box (VERTICAL, 0);
123131
popover_content.add (lang_selection_filter);
124132
popover_content.add (lang_scrolled);
125-
126133
popover_content.show_all ();
127134

128-
var lang_popover = new Gtk.Popover (lang_menubutton) {
129-
position = Gtk.PositionType.BOTTOM,
135+
var lang_popover = new Gtk.Popover (null) {
136+
position = BOTTOM,
130137
child = popover_content
131138
};
132-
lang_menubutton.popover = lang_popover;
133139

134-
lang_selection_listbox.row_activated.connect ((row) => {
135-
var lang_entry = ((LangEntry) row);
136-
select_language (lang_entry);
137-
});
138-
}
139-
140-
private void select_language (LangEntry lang, bool update_source_view = true) {
141-
lang_selection_listbox.select_row (lang);
142-
lang_menubutton.text = lang.lang_name;
143-
if (update_source_view) {
144-
lang.active = true;
145-
doc.source_view.language = lang.lang_id != null ? manager.get_language (lang.lang_id) : null;
146-
} else {
147-
lang.selected = true;
148-
}
149-
}
150-
151-
private void create_tabulation_popover () {
152-
editorconfig_infobar = new Gtk.InfoBar () {
153-
margin_top = 9,
154-
margin_end = 9,
155-
margin_start = 9
140+
lang_menubutton = new FormatButton () {
141+
icon = new ThemedIcon ("application-x-class-file-symbolic"),
142+
popover = lang_popover,
143+
tooltip_text = _("Document language")
156144
};
157-
editorconfig_infobar.get_content_area ().add (new Gtk.Label (_("Some settings set by EditorConfig file")));
158-
editorconfig_infobar.get_style_context ().add_class (Gtk.STYLE_CLASS_FRAME);
159145

160-
var autoindent_modelbutton = new Granite.SwitchModelButton (_("Automatic Indentation"));
161-
162-
space_tab_modelbutton = new Granite.SwitchModelButton (_("Insert Spaces Instead Of Tabs"));
146+
goto_entry = new Gtk.Entry ();
163147

164-
var width_label = new Gtk.Label (_("Tab width")) {
165-
halign = Gtk.Align.START,
166-
hexpand = true
148+
var goto_label = new Gtk.Label (_("Go To Line:")) {
149+
mnemonic_widget = goto_entry
167150
};
168151

169-
width_spinbutton = new Gtk.SpinButton.with_range (2, 16, 1);
170-
171-
tab_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12) {
172-
margin_top = 6,
173-
margin_end = 12,
152+
var line_box = new Gtk.Box (HORIZONTAL, 12) {
153+
margin_top = 12,
154+
margin_bottom = 12,
174155
margin_start = 12,
156+
margin_end = 12
175157
};
176-
tab_box.add (width_label);
177-
tab_box.add (width_spinbutton);
158+
line_box.add (goto_label);
159+
line_box.add (goto_entry);
160+
line_box.show_all ();
178161

179-
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0) {
180-
margin_bottom = 12
162+
var line_popover = new Gtk.Popover (null) {
163+
position = BOTTOM,
164+
child = line_box
181165
};
182-
box.add (editorconfig_infobar);
183-
box.add (autoindent_modelbutton);
184-
box.add (space_tab_modelbutton);
185-
box.add (tab_box);
186-
box.show_all ();
187166

188-
var tab_popover = new Gtk.Popover (tab_menubutton) {
189-
position = Gtk.PositionType.BOTTOM,
190-
child = box
167+
line_menubutton = new FormatButton () {
168+
icon = new ThemedIcon ("view-continuous-symbolic"),
169+
popover = line_popover
191170
};
192-
tab_menubutton.popover = tab_popover;
171+
line_menubutton.tooltip_markup = Granite.markup_accel_tooltip (
172+
((Scratch.Application) GLib.Application.get_default ()).get_accels_for_action (
173+
Scratch.MainWindow.ACTION_PREFIX + Scratch.MainWindow.ACTION_GO_TO
174+
),
175+
_("Line number")
176+
);
193177

194-
Scratch.settings.changed["indent-width"].connect (format_tab_header_from_global_settings);
195-
Scratch.settings.changed["spaces-instead-of-tabs"].connect (format_tab_header_from_global_settings);
196-
Scratch.settings.bind ("auto-indent", autoindent_modelbutton, "active", SettingsBindFlags.DEFAULT);
178+
homogeneous = true;
179+
add (tab_menubutton);
180+
add (lang_menubutton);
181+
add (line_menubutton);
182+
183+
lang_selection_listbox.row_activated.connect ((row) => {
184+
var lang_entry = ((LangEntry) row);
185+
select_language (lang_entry);
186+
});
187+
188+
lang_selection_filter.changed.connect (() => {
189+
lang_selection_listbox.invalidate_filter ();
190+
});
197191

198192
format_tab_header_from_global_settings ();
199193
width_spinbutton.value_changed.connect (() => {
@@ -213,6 +207,37 @@ public class Code.FormatBar : Gtk.Box {
213207
);
214208
}
215209
});
210+
211+
// We need to connect_after because otherwise, the text isn't parsed into the "value" property and we only get the previous value
212+
goto_entry.activate.connect_after (() => {
213+
int line, column;
214+
goto_entry.text = goto_entry.text.replace (":", ".");
215+
goto_entry.text.scanf ("%i.%i", out line, out column);
216+
doc.source_view.go_to_line (line, column - 1);
217+
// Focuses parent to the source view, so that the cursor, which indicates line and column is actually visible.
218+
doc.source_view.grab_focus ();
219+
});
220+
221+
Scratch.settings.changed["indent-width"].connect (format_tab_header_from_global_settings);
222+
Scratch.settings.changed["spaces-instead-of-tabs"].connect (format_tab_header_from_global_settings);
223+
Scratch.settings.bind ("auto-indent", autoindent_modelbutton, "active", DEFAULT);
224+
225+
bind_property ("tab-width-set-by-editor-config", tab_box, "sensitive", INVERT_BOOLEAN | SYNC_CREATE);
226+
}
227+
228+
public void activate_line_menubutton () {
229+
line_menubutton.active = true;
230+
}
231+
232+
private void select_language (LangEntry lang, bool update_source_view = true) {
233+
lang_selection_listbox.select_row (lang);
234+
lang_menubutton.text = lang.lang_name;
235+
if (update_source_view) {
236+
lang.active = true;
237+
doc.source_view.language = lang.lang_id != null ? manager.get_language (lang.lang_id) : null;
238+
} else {
239+
lang.selected = true;
240+
}
216241
}
217242

218243
private void format_tab_header_from_global_settings () {
@@ -226,7 +251,6 @@ public class Code.FormatBar : Gtk.Box {
226251

227252
editorconfig_infobar.revealed = tab_style_set_by_editor_config || tab_width_set_by_editor_config;
228253
space_tab_modelbutton.sensitive = !tab_style_set_by_editor_config;
229-
tab_box.sensitive = !tab_width_set_by_editor_config;
230254
}
231255

232256
private void format_line_header () {
@@ -239,40 +263,6 @@ public class Code.FormatBar : Gtk.Box {
239263
goto_entry.text = "%d.%d".printf (line, iter.get_line_offset () + 1);
240264
}
241265

242-
private void create_line_popover () {
243-
var goto_label = new Gtk.Label (_("Go To Line:"));
244-
goto_label.xalign = 1;
245-
246-
goto_entry = new Gtk.Entry ();
247-
248-
var line_grid = new Gtk.Grid () {
249-
margin_top = 12,
250-
margin_bottom = 12,
251-
margin_start = 12,
252-
margin_end = 12
253-
};
254-
line_grid.column_spacing = 12;
255-
line_grid.attach (goto_label, 0, 0, 1, 1);
256-
line_grid.attach (goto_entry, 1, 0, 1, 1);
257-
line_grid.show_all ();
258-
259-
var line_popover = new Gtk.Popover (line_menubutton) {
260-
position = Gtk.PositionType.BOTTOM,
261-
child = line_grid
262-
};
263-
line_menubutton.popover = line_popover;
264-
265-
// We need to connect_after because otherwise, the text isn't parsed into the "value" property and we only get the previous value
266-
goto_entry.activate.connect_after (() => {
267-
int line, column;
268-
goto_entry.text = goto_entry.text.replace (":", ".");
269-
goto_entry.text.scanf ("%i.%i", out line, out column);
270-
doc.source_view.go_to_line (line, column - 1);
271-
// Focuses parent to the source view, so that the cursor, which indicates line and column is actually visible.
272-
doc.source_view.grab_focus ();
273-
});
274-
}
275-
276266
public void set_document (Scratch.Services.Document doc) {
277267
if (this.doc != null) {
278268
this.doc.source_view.buffer.notify["cursor-position"].disconnect (format_line_header);

0 commit comments

Comments
 (0)