Debian Codegraph @

src/display-view.vala

raw
1 /* display-view.vala
2 *
3 * Copyright (C) Red Hat, Inc
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Felipe Borges <[email protected]>
19 *
20 */
21
22 using Gtk;
23 using Gdk;
24
25 namespace Connections {
26 [GtkTemplate (ui = "/org/gnome/Connections/ui/display-view.ui")]
27 private class DisplayView : Gtk.Box {
28 [GtkChild]
29 private unowned EventBox event_box;
30
31 [GtkChild]
32 private unowned Stack stack;
33
34 [GtkChild]
35 private unowned Label size_label;
36 [GtkChild]
37 private unowned Button escape_fullscreen_button;
38
39 private Connection? connection;
40
41 private ulong show_display_id;
42
43 construct {
44 event_box.set_events (EventMask.POINTER_MOTION_MASK |
45 EventMask.SCROLL_MASK |
46 EventMask.SMOOTH_SCROLL_MASK);
47 }
48
49 private void remove_display () {
50 var widget = event_box.get_child ();
51 if (widget != null)
52 event_box.remove (widget);
53
54 if (connection == null)
55 return;
56
57 connection.dispose_display ();
58 connection = null;
59 }
60
61 public void replace_display (Connection connection) {
62 if (event_box.get_child () == connection.widget)
63 return;
64
65 remove_display ();
66
67 this.connection = connection;
68 var display = connection.widget;
69
70 event_box.add (display);
71 event_box.show_all ();
72
73 ulong draw_id = 0;
74 draw_id = display.draw.connect (() => {
75 display.disconnect (draw_id);
76
77 display.get_window ().notify["cursor"].connect (() => {
78 event_box.get_window ().set_cursor (display.get_window ().get_cursor ());
79 });
80
81 return false;
82 });
83 }
84
85 public void connect_to (Connection connection) {
86 stack.set_visible_child_name ("loading");
87 switch_to (connection);
88 connection.connect_it ();
89 }
90
91 public void switch_to (Connection connection) {
92 if (show_display_id != 0) {
93 this.connection.disconnect (show_display_id);
94 show_display_id = 0;
95 }
96
97 replace_display (connection);
98
99 show_display_id = connection.show.connect (show_display);
100 }
101
102 private void show_display () {
103 stack.set_visible_child_name ("display");
104 }
105
106 [GtkCallback]
107 private bool on_event_box_scroll_event (Gdk.EventScroll scroll_event) {
108 if (scroll_event.type == EventType.GRAB_BROKEN)
109 return false;
110
111 if (event_box.get_child () != null) {
112 if ((event_box.get_child ().get_events () & EventMask.SMOOTH_SCROLL_MASK) == 0) {
113 if (scroll_event.delta_x >= 0.5)
114 scroll_event.direction = ScrollDirection.RIGHT;
115 else if (scroll_event.delta_x <= -0.5)
116 scroll_event.direction = ScrollDirection.LEFT;
117
118 if (scroll_event.delta_y >= 0.5)
119 scroll_event.direction = ScrollDirection.DOWN;
120 else if (scroll_event.delta_y <= -0.5)
121 scroll_event.direction = ScrollDirection.UP;
122 }
123
124 event_box.get_child ().event (scroll_event);
125 }
126
127 return false;
128 }
129
130 [GtkCallback]
131 private bool on_event_box_event (Gdk.Event event) {
132 if (event.type == EventType.GRAB_BROKEN ||
133 event.type == EventType.SCROLL)
134 return false;
135
136 if (event_box.get_child () != null)
137 event_box.get_child ().event (event);
138
139 return false;
140 }
141
142 private uint size_label_timeout;
143 private int width = -1;
144 private int height = -1;
145 [GtkCallback]
146 private void on_size_allocate (Gtk.Allocation allocation) {
147 if (width == allocation.width && height == allocation.height) {
148 return;
149 }
150
151 width = allocation.width;
152 height = allocation.height;
153
154 // Translators: Showing size of widget as WIDTH×HEIGHT here.
155 size_label.label = _("%d×%d").printf (allocation.width, allocation.height);
156
157 Idle.add (() => {
158 size_label.visible = true;
159
160 if (size_label_timeout != 0) {
161 Source.remove (size_label_timeout);
162 size_label_timeout = 0;
163 }
164
165 size_label_timeout = Timeout.add_seconds (3, () => {
166 size_label.visible = false;
167 size_label_timeout = 0;
168
169 return false;
170 });
171
172 return false;
173 });
174 }
175
176 [GtkCallback]
177 private void on_escape_fullscreen_button_clicked () {
178 Application.application.main_window.fullscreened = false;
179 escape_fullscreen_button.visible = false;
180 }
181
182 public override bool motion_notify_event (Gdk.EventMotion event) {
183 if (!Application.application.main_window.fullscreened)
184 return false;
185
186 escape_fullscreen_button.visible = (event.y < 40);
187
188 return base.motion_notify_event (event);
189 }
190 }
191 }