-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathpreviewer.vala
More file actions
252 lines (208 loc) · 7.64 KB
/
previewer.vala
File metadata and controls
252 lines (208 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
namespace Workbench {
[DBus (name="re.sonny.Workbench.previewer_module")]
public class Previewer : Object {
construct {
this.notify["ColorScheme"].connect (() => {
this.style_manager.color_scheme = this.ColorScheme;
});
}
private void ensure_window() {
if (this.window != null) {
return;
}
var window = new Gtk.Window () {
// Ensure the header bar has the same height as the one on Workbench main window
titlebar = new Gtk.HeaderBar (),
title = "Preview",
default_width = 600,
default_height = 800
};
this.set_window(window);
}
private void set_window(Gtk.Window window) {
this.window?.destroy ();
this.window = window;
this.window.close_request.connect (this.on_window_closed);
}
private bool on_window_closed () {
this.window_open (false);
this.window = null;
return false;
}
public bool screenshot (string path) {
Gtk.Widget widget = this.target;
var paintable = new Gtk.WidgetPaintable (widget);
int width = widget.get_allocated_width ();
int height = widget.get_allocated_height ();
var snapshot = new Gtk.Snapshot ();
paintable.snapshot (snapshot, width, height);
Gsk.RenderNode? node = snapshot.to_node ();
if (node == null) {
debug (@"Could not get node snapshot, width: $width, height: $height");
return false;
}
Gsk.Renderer? renderer = widget.get_native ()?.get_renderer ();
var rect = Graphene.Rect () {
origin = Graphene.Point.zero (),
size = Graphene.Size () {
width = width,
height = height
}
};
Gdk.Texture texture = renderer.render_texture (node, rect);
texture.save_to_png (path);
return true;
}
// This registers GObjects so that they can be found by GtkBuilder
void ensure_types() {
typeof (Shumate.SimpleMap).ensure();
typeof (WebKit.WebView).ensure();
}
public void update_ui (string content, string target_id, string original_id = "", string template_gtype_name = "", string template = "") {
// we don't use template: This is compiled as a gresource for Vala and for Rust it's read directly
// and compiled from a path to the UI file. See Rust/Vala compiler.
this.ensure_types();
this.builder = new Gtk.Builder.from_string (content, content.length);
var target = this.builder.get_object (target_id) as Gtk.Widget;
if (target == null) {
stderr.printf (@"Widget with target_id='$target_id' could not be found.\n");
return;
}
this.target = target;
if (original_id != "") {
this.builder.expose_object(original_id, target);
}
// Not a Root/Window
if (!(target is Gtk.Root)) {
this.ensure_window();
this.window.child = target;
return;
}
// Set target as window directly
if (this.window == null || this.window.get_type () != target.get_type ()) {
this.set_window(target as Gtk.Window);
return;
}
if (target is Adw.Window) {
var child = ((Adw.Window) target).content;
((Adw.Window) target).content = null;
((Adw.Window) this.window).content = child;
} else if (target is Adw.ApplicationWindow) {
var child = ((Adw.ApplicationWindow) target).content;
((Adw.ApplicationWindow) target).content = null;
((Adw.ApplicationWindow) this.window).content = child;
} else if (target is Gtk.Window) {
var child = ((Gtk.Window) target).child;
((Gtk.Window) target).child = null;
this.window.child = child;
}
// Toplevel windows returned by these functions will stay around
// until the user explicitly destroys them with gtk_window_destroy().
// https://docs.gtk.org/gtk4/class.Builder.html
if (target is Gtk.Window) {
((Gtk.Window) target).destroy();
}
}
public void update_css (string content) {
if (this.css != null)
Gtk.StyleContext.remove_provider_for_display (Gdk.Display.get_default (), this.css);
this.css = new Gtk.CssProvider ();
this.css.parsing_error.connect((self, section, error) => {
var start = section.get_start_location();
var end = section.get_end_location();
this.css_parser_error(error.message, (int)start.lines, (int)start.line_chars, (int)end.lines, (int)end.line_chars);
});
this.css.load_from_data (content.data);
Gtk.StyleContext.add_provider_for_display (
Gdk.Display.get_default (),
this.css,
// STYLE_PROVIDER_PRIORITY_THEME is 200; only values below that behave correctly
Gtk.STYLE_PROVIDER_PRIORITY_THEME - 1);
}
public void run (string filename, string uri) {
if (this.module != null) {
this.module.close ();
}
this.module = Module.open (filename, ModuleFlags.LAZY);
if (this.module == null) {
stderr.printf ("Module loading failed.\n");
return;
}
void* function;
this.module.symbol ("set_base_uri", out function);
var set_base_uri = (BaseUriFunction) function;
set_base_uri (uri);
this.module.symbol ("set_builder", out function);
var set_builder = (BuilderFunction) function;
if (this.builder != null) {
set_builder (this.builder);
}
this.module.symbol ("set_window", out function);
var set_window = (WindowFunction) function;
if (this.window != null) {
set_window (this.window);
}
this.module.symbol ("main", out function);
if (function == null) {
stderr.printf (@"Function 'main' not found.\n");
return;
}
var main_function = (MainFunction) function;
main_function ();
}
public void close_window () {
if (this.window == null) {
return;
}
this.window.close ();
}
public async void open_window (int width, int height) {
this.window.default_width = width;
this.window.default_height = height;
this.window.present ();
this.window_open (true);
}
public void enable_inspector (bool enabled) {
Gtk.Window.set_interactive_debugging (enabled);
}
public void preview() {
}
public Adw.ColorScheme ColorScheme { get; set; default = Adw.ColorScheme.DEFAULT; }
public signal void window_open (bool open);
public signal void css_parser_error (string message, int start_line, int start_char, int end_line, int end_char);
[CCode (has_target=false)]
private delegate void MainFunction ();
[CCode (has_target=false)]
private delegate void BuilderFunction (Gtk.Builder builder);
[CCode (has_target=false)]
private delegate void WindowFunction (Gtk.Window window);
[CCode (has_target=false)]
private delegate void BaseUriFunction (string uri);
private Gtk.Window? window;
private Gtk.Widget? target;
private Gtk.CssProvider? css = null;
private Module module;
private Gtk.Builder? builder = null;
private Adw.StyleManager style_manager = Adw.StyleManager.get_default ();
}
void main (string[] args) {
if (!Module.supported ()) {
stderr.printf ("This system does not support loadable modules.\n");
Process.exit (1);
}
var loop = new MainLoop();
Adw.init();
var connection = new DBusConnection.for_address_sync(
args[1],
DBusConnectionFlags.AUTHENTICATION_CLIENT,
null,
null
);
var previewer = new Previewer ();
connection.on_closed.connect(() => {
loop.quit();
});
connection.register_object ("/re/sonny/workbench/previewer_module", previewer);
loop.run();
}
}