Merge lp:~artem-anufrij/scratch/terminal-save-settings into lp:~elementary-apps/scratch/scratch

Proposed by Artem Anufrij
Status: Merged
Approved by: Danielle Foré
Approved revision: 1447
Merged at revision: 1456
Proposed branch: lp:~artem-anufrij/scratch/terminal-save-settings
Merge into: lp:~elementary-apps/scratch/scratch
Diff against target: 287 lines (+121/-30)
6 files modified
plugins/CMakeLists.txt (+1/-1)
plugins/terminal/CMakeLists.txt (+2/-0)
plugins/terminal/Settings.vala (+41/-0)
plugins/terminal/terminal.vala (+60/-28)
schemas/CMakeLists.txt (+2/-1)
schemas/org.pantheon.scratch.plugins.terminal.gschema.xml (+15/-0)
To merge this branch: bzr merge lp:~artem-anufrij/scratch/terminal-save-settings
Reviewer Review Type Date Requested Status
Robert Roth code style Pending
elementary UX Pending
Review via email: mp+250398@code.launchpad.net

This proposal supersedes a proposal from 2015-01-11.

Commit message

- added settings for the terminal plugin
- terminal: save position (bottom / right) of the terminal plugin
- terminal: save last opened path

Description of the change

- added settings for the terminal plugin
- terminal: save position (bottom / right) of the terminal plugin
- terminal: save last opened path

To post a comment you must log in.
Revision history for this message
Robert Roth (evfool) wrote : Posted in a previous version of this proposal

I get a new warning while building this:

terminal-save-settings/plugins/terminal/terminal.vala:200.34-200.48: warning: Access to static member `Scratch.Plugins.TerminalViewer.TerminalPosition.BOTTOM' with an instance reference
        if (settings.position == position.BOTTOM)

This should be an easy fix, other then this everything looks fine (can not compile right now because of some problems with the word-completion plugin).

review: Approve (code style)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/CMakeLists.txt'
2--- plugins/CMakeLists.txt 2014-11-05 23:56:27 +0000
3+++ plugins/CMakeLists.txt 2015-02-20 00:48:24 +0000
4@@ -13,4 +13,4 @@
5 add_subdirectory (outline)
6 add_subdirectory (vim-emulation)
7 add_subdirectory (highlight-word-selection)
8-add_subdirectory (clipboard-history)
9\ No newline at end of file
10+add_subdirectory (clipboard-history)
11
12=== modified file 'plugins/terminal/CMakeLists.txt'
13--- plugins/terminal/CMakeLists.txt 2014-11-20 11:50:45 +0000
14+++ plugins/terminal/CMakeLists.txt 2015-02-20 00:48:24 +0000
15@@ -14,6 +14,7 @@
16
17 if (${VTE_VERSION} VERSION_LESS "0.38.0")
18 vala_precompile(VALA_C
19+ Settings.vala
20 terminal.vala
21 PACKAGES
22 vte-2.90
23@@ -32,6 +33,7 @@
24 )
25 else()
26 vala_precompile(VALA_C
27+ Settings.vala
28 terminal.vala
29 PACKAGES
30 vte-2.91
31
32=== added file 'plugins/terminal/Settings.vala'
33--- plugins/terminal/Settings.vala 1970-01-01 00:00:00 +0000
34+++ plugins/terminal/Settings.vala 2015-02-20 00:48:24 +0000
35@@ -0,0 +1,41 @@
36+// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
37+/***
38+ BEGIN LICENSE
39+
40+ Copyright (C) 2015 Artem Anufrij <artem.anufrij@live.de>
41+ This program is free software: you can redistribute it and/or modify it
42+ under the terms of the GNU Lesser General Public License version 3, as published
43+ by the Free Software Foundation.
44+
45+ This program is distributed in the hope that it will be useful, but
46+ WITHOUT ANY WARRANTY; without even the implied warranties of
47+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
48+ PURPOSE. See the GNU General Public License for more details.
49+
50+ You should have received a copy of the GNU General Public License along
51+ with this program. If not, see <http://www.gnu.org/licenses/>
52+
53+ END LICENSE
54+***/
55+
56+namespace Scratch.Plugins.TerminalViewer {
57+
58+ public enum TerminalPosition {
59+ BOTTOM = 0,
60+ RIGHT = 1
61+ }
62+ /**
63+ * Class for interacting with gsettings.
64+ */
65+ internal class Settings : Granite.Services.Settings {
66+
67+ private const string SCHEMA = "org.pantheon.scratch.plugins.terminal";
68+
69+ public int position { get; set; }
70+ public string last_opened_path { get; set; }
71+
72+ public Settings () {
73+ base (SCHEMA);
74+ }
75+ }
76+}
77
78=== modified file 'plugins/terminal/terminal.vala'
79--- plugins/terminal/terminal.vala 2014-12-16 17:16:17 +0000
80+++ plugins/terminal/terminal.vala 2015-02-20 00:48:24 +0000
81@@ -27,19 +27,21 @@
82
83 MainWindow window = null;
84
85+ Scratch.Plugins.TerminalViewer.Settings settings;
86+
87 Gtk.Notebook? bottombar = null;
88 Gtk.Notebook? contextbar = null;
89 Scratch.Widgets.Toolbar? toolbar = null;
90 Gtk.ToggleToolButton? tool_button = null;
91
92- bool on_bottom = true;
93-
94 Gtk.RadioMenuItem location_bottom = null;
95 Gtk.RadioMenuItem location_right = null;
96
97 Vte.Terminal terminal;
98 Gtk.Grid grid;
99
100+ GLib.Pid child_pid;
101+
102 Scratch.Services.Interface plugins;
103 public Object object { owned get; construct; }
104
105@@ -56,6 +58,8 @@
106
107 window = w;
108 window.key_press_event.connect (switch_focus);
109+ window.destroy.connect (save_last_working_directory);
110+
111 });
112
113 plugins.hook_notebook_bottom.connect ((n) => {
114@@ -93,29 +97,28 @@
115 public void deactivate () {
116 if (terminal != null)
117 grid.destroy ();
118+
119 if (tool_button != null)
120 tool_button.destroy ();
121
122 window.key_press_event.disconnect (switch_focus);
123- }
124-
125- void switch_terminal_location () {
126-
127- if (bottombar.page_num (grid) == -1 && this.location_bottom.active) {
128-
129- contextbar.remove_page (contextbar.page_num (grid));
130- bottombar.set_current_page (bottombar.append_page (grid, new Gtk.Label (_("Terminal"))));
131- on_bottom = true;
132- debug ("Move Terminal: BOTTOMBAR.");
133-
134- } else if (contextbar.page_num (grid) == -1 && this.location_right.active) {
135-
136- bottombar.remove_page (bottombar.page_num (grid));
137- contextbar.set_current_page (contextbar.append_page (grid, new Gtk.Label (_("Terminal"))));
138- on_bottom = false;
139- debug ("Move Terminal: CONTEXTBAR.");
140-
141- }
142+ window.destroy.disconnect (save_last_working_directory);
143+ }
144+
145+ void save_last_working_directory () {
146+ settings.last_opened_path = get_shell_location ();
147+ }
148+
149+ void move_terminal_bottombar () {
150+ contextbar.remove_page (contextbar.page_num (grid));
151+ bottombar.set_current_page (bottombar.append_page (grid, new Gtk.Label (_("Terminal"))));
152+ debug ("Move Terminal: BOTTOMBAR.");
153+ }
154+
155+ void move_terminal_contextbar () {
156+ bottombar.remove_page (bottombar.page_num (grid));
157+ contextbar.set_current_page (contextbar.append_page (grid, new Gtk.Label (_("Terminal"))));
158+ debug ("Move Terminal: CONTEXTBAR.");
159 }
160
161 bool switch_focus (Gdk.EventKey event) {
162@@ -159,18 +162,20 @@
163 tool_button.toggled.connect (() => {
164 if (this.tool_button.active) {
165 tool_button.tooltip_text = _("Hide Terminal");
166- if (on_bottom) {
167+ if (settings.position == Scratch.Plugins.TerminalViewer.TerminalPosition.BOTTOM) {
168 bottombar.set_current_page (bottombar.append_page (grid, new Gtk.Label (_("Terminal"))));
169 } else {
170 contextbar.set_current_page (contextbar.append_page (grid, new Gtk.Label (_("Terminal"))));
171 }
172+ terminal.grab_focus ();
173 } else {
174 tool_button.tooltip_text = _("Show Terminal");
175- if (on_bottom) {
176+ if (settings.position == Scratch.Plugins.TerminalViewer.TerminalPosition.BOTTOM) {
177 bottombar.remove_page (bottombar.page_num (grid));
178 } else {
179 contextbar.remove_page (contextbar.page_num (grid));
180 }
181+ window.get_current_document ().focus ();
182 }
183 });
184
185@@ -179,7 +184,27 @@
186 toolbar.pack_end (tool_button);
187 }
188
189+ public string get_shell_location () {
190+ int pid = (!) (this.child_pid);
191+
192+ try {
193+ return GLib.FileUtils.read_link ("/proc/%d/cwd".printf (pid));
194+ } catch (GLib.FileError error) {
195+ warning ("An error occured while fetching the current dir of shell");
196+ return "";
197+ }
198+ }
199+
200+ public void settings_changed () {
201+ if (settings.position == Scratch.Plugins.TerminalViewer.TerminalPosition.BOTTOM)
202+ move_terminal_bottombar ();
203+ else
204+ move_terminal_contextbar ();
205+ }
206+
207 void on_hook_notebook () {
208+ this.settings = new Scratch.Plugins.TerminalViewer.Settings ();
209+ settings.changed.connect (settings_changed);
210 this.terminal = new Vte.Terminal ();
211 this.terminal.scrollback_lines = -1;
212
213@@ -291,18 +316,24 @@
214 // ON RIGHT
215 location_right = new Gtk.RadioMenuItem.with_label (null, _("Terminal on Right"));
216 location_right.toggled.connect (() => {
217- switch_terminal_location ();
218+ if (settings.position != Scratch.Plugins.TerminalViewer.TerminalPosition.RIGHT)
219+ settings.position = Scratch.Plugins.TerminalViewer.TerminalPosition.RIGHT;
220 });
221 menu.append (location_right);
222
223 // ON BOTTOM
224 location_bottom = new Gtk.RadioMenuItem.with_label (location_right.get_group (), _("Terminal on Bottom"));
225- location_bottom.active = true;
226 location_bottom.toggled.connect (() => {
227- switch_terminal_location ();
228+ if (settings.position != Scratch.Plugins.TerminalViewer.TerminalPosition.BOTTOM)
229+ settings.position = Scratch.Plugins.TerminalViewer.TerminalPosition.BOTTOM;
230 });
231 menu.append (location_bottom);
232
233+ if (settings.position == Scratch.Plugins.TerminalViewer.TerminalPosition.BOTTOM)
234+ location_bottom.active = true;
235+ else
236+ location_right.active = true;
237+
238 menu.show_all ();
239
240 this.terminal.button_press_event.connect ((event) => {
241@@ -314,10 +345,11 @@
242 });
243
244 try {
245+ string last_opened_path = settings.last_opened_path == "" ? "~/" : settings.last_opened_path;
246 #if ! VTE291
247- this.terminal.fork_command_full (Vte.PtyFlags.DEFAULT, "~/", { Vte.get_user_shell () }, null, GLib.SpawnFlags.SEARCH_PATH, null, null);
248+ this.terminal.fork_command_full (Vte.PtyFlags.DEFAULT, last_opened_path, { Vte.get_user_shell () }, null, GLib.SpawnFlags.SEARCH_PATH, null, out child_pid);
249 #else
250- this.terminal.spawn_sync (Vte.PtyFlags.DEFAULT, "~/", { Vte.get_user_shell () }, null, GLib.SpawnFlags.SEARCH_PATH, null, null, null);
251+ this.terminal.spawn_sync (Vte.PtyFlags.DEFAULT, last_opened_path, { Vte.get_user_shell () }, null, GLib.SpawnFlags.SEARCH_PATH, null, null, out child_pid);
252 #endif
253 } catch (GLib.Error e) {
254 warning (e.message);
255
256=== modified file 'schemas/CMakeLists.txt'
257--- schemas/CMakeLists.txt 2013-07-16 15:56:38 +0000
258+++ schemas/CMakeLists.txt 2015-02-20 00:48:24 +0000
259@@ -1,4 +1,5 @@
260 include(GSettings)
261 add_schema("org.pantheon.scratch.gschema.xml")
262 add_schema("org.pantheon.scratch.plugins.folder-manager.gschema.xml")
263-add_schema("org.pantheon.scratch.plugins.file-manager.gschema.xml")
264\ No newline at end of file
265+add_schema("org.pantheon.scratch.plugins.file-manager.gschema.xml")
266+add_schema("org.pantheon.scratch.plugins.terminal.gschema.xml")
267\ No newline at end of file
268
269=== added file 'schemas/org.pantheon.scratch.plugins.terminal.gschema.xml'
270--- schemas/org.pantheon.scratch.plugins.terminal.gschema.xml 1970-01-01 00:00:00 +0000
271+++ schemas/org.pantheon.scratch.plugins.terminal.gschema.xml 2015-02-20 00:48:24 +0000
272@@ -0,0 +1,15 @@
273+<?xml version="1.0" encoding="UTF-8"?>
274+<schemalist>
275+ <schema path="/org/pantheon/scratch/plugins/terminal/" id="org.pantheon.scratch.plugins.terminal" gettext-domain="scratch">
276+ <key name="position" type="i">
277+ <default>0</default>
278+ <summary>Terminal position</summary>
279+ <description>0: Position on bottom bar. 1: Postion on right bar.</description>
280+ </key>
281+ <key name="last-opened-path" type="s">
282+ <default>''</default>
283+ <summary>Last opened path</summary>
284+ <description>Last opened path in the terminal plugin</description>
285+ </key>
286+ </schema>
287+</schemalist>

Subscribers

People subscribed via source and target branches