[ease] [general] Signal-ify Main a bit.



commit fc282f71cc4a0ac42aa2beac619e7e7ae0079264
Author: Nate Stedman <natesm gmail com>
Date:   Sat Aug 7 02:40:12 2010 -0400

    [general] Signal-ify Main a bit.

 ease/ease-editor-window.vala  |   39 ++++++++++++++++++----------
 ease/ease-main.vala           |   55 ++++++++++++++++++++++++++++++++++++++--
 ease/ease-welcome-window.vala |   22 ++--------------
 3 files changed, 80 insertions(+), 36 deletions(-)
---
diff --git a/ease/ease-editor-window.vala b/ease/ease-editor-window.vala
index c135c33..920b8e8 100644
--- a/ease/ease-editor-window.vala
+++ b/ease/ease-editor-window.vala
@@ -127,6 +127,16 @@ internal class Ease.EditorWindow : Gtk.Window
 	long last_saved = 0;
 	
 	/**
+	 * Emitted when the window's Document should be presented.
+	 */
+	internal signal void play(Document doc);
+	
+	/**
+	 * Emitted when the window should be closed.
+	 */
+	internal signal void close(EditorWindow self);
+	
+	/**
 	 * The zoom levels for the { link ZoomSlider}
 	 */
 	private int[] ZOOM_LEVELS = {10, 25, 33, 50, 66, 75, 100, 125, 150,
@@ -223,7 +233,11 @@ internal class Ease.EditorWindow : Gtk.Window
 		
 		// close the window
 		delete_event.connect((sender, event) => {
-			if (last_saved == 0) return false;
+			if (last_saved == 0)
+			{
+				close(this);
+				return false;
+			}
 			
 			var name = document.filename == null ? _("Untitled Document") :
 			                                       document.filename;
@@ -236,14 +250,19 @@ internal class Ease.EditorWindow : Gtk.Window
 			if (response == Gtk.ResponseType.CANCEL) return true;
 			if (response == Gtk.ResponseType.NO)
 			{
-				Main.remove_window(this);
+				close(this);
 				return false;
 			}
 			
 			// otherwise, save and quit
-			var result = !save_document(null);
-			if (!result) Main.remove_window(this);
-			return result;
+			if (save_document(null))
+			{
+				close(this);
+				return false;
+			}
+			
+			// there were errors, don't close the window and lose the document
+			return true;
 		});
 		
 		set_slide(0);
@@ -354,15 +373,7 @@ internal class Ease.EditorWindow : Gtk.Window
 	[CCode (instance_pos = -1)]
 	internal void play_handler(Gtk.Widget sender)
 	{		
-		player = new Player(document);
-		
-		player.complete.connect(() => {
-			player = null;
-			show();
-			present();
-		});
-		
-		hide();
+		play(document);
 	}
 	
 	[CCode (instance_pos = -1)]
diff --git a/ease/ease-main.vala b/ease/ease-main.vala
index 2b7c79d..b89aa76 100644
--- a/ease/ease-main.vala
+++ b/ease/ease-main.vala
@@ -150,7 +150,6 @@ internal class Ease.Main : GLib.Object
 			if (w.document.path == path)
 			{
 				w.present();
-				
 				return;
 			}
 		}
@@ -166,6 +165,24 @@ internal class Ease.Main : GLib.Object
 			return;
 		}
 	}
+	
+	/**
+	 * Creates a new { link EditorWindow} from a theme and size.
+	 */
+	internal static void new_from_theme(Theme theme, int width, int height)
+	{
+		try
+		{
+			var document = new Document.from_theme(theme, width, height);
+			var editor = new EditorWindow(document);
+			add_window(editor);
+			remove_welcome();
+		}
+		catch (Error e)
+		{
+			error_dialog(_("Error creating new document"), e.message);
+		}
+	}
 
 	/**
 	 * Removes an { link EditorWindow} from Ease's internal store of windows.
@@ -177,9 +194,12 @@ internal class Ease.Main : GLib.Object
 	 *
 	 * @param win The { link EditorWindow}.
 	 */
-	internal static void remove_window(EditorWindow win)
+	private static void remove_window(EditorWindow win)
 	{
 		windows.remove(win);
+		win.play.disconnect(on_play);
+		win.close.disconnect(on_close);
+		
 		if (windows.size == 0 && welcome == null)
 		{
 			Gtk.main_quit();
@@ -195,9 +215,38 @@ internal class Ease.Main : GLib.Object
 	 *
 	 * @param win The { link EditorWindow}.
 	 */
-	internal static void add_window(EditorWindow win)
+	private static void add_window(EditorWindow win)
 	{
 		windows.add(win);
+		win.play.connect(on_play);
+		win.close.connect(on_close);
+	}
+	
+	/**
+	 * Handles the { link EditorWindow.play} signal.
+	 *
+	 * Hides all visible windows and displays the presentation.
+	 */
+	private static void on_play(Document document)
+	{
+		player = new Player(document);
+		player.present();
+		
+		player.complete.connect(() => {
+			player = null;
+			foreach (var window in windows) window.show();
+		});
+		
+		foreach (var window in windows) window.hide();
+	}
+	
+	/**
+	 * Closes and removes an EditorWindow.
+	 */
+	private static void on_close(EditorWindow self)
+	{
+		self.hide();
+		remove_window(self);
 	}
 
 	/**
diff --git a/ease/ease-welcome-window.vala b/ease/ease-welcome-window.vala
index bd9e148..1cb08e2 100644
--- a/ease/ease-welcome-window.vala
+++ b/ease/ease-welcome-window.vala
@@ -275,25 +275,9 @@ internal class Ease.WelcomeWindow : Gtk.Window
 	[CCode (instance_pos = -1)]
 	internal void create_new_document(Gtk.Widget? sender)
 	{
-		try
-		{
-			// create a new Document
-			// FIXME : this call crashes. We don't reach the next line,
-			// nor do we reach the constructor from_theme(). It must have
-			// something to do with the closure (gdb talks about it).
-			var document = new Document.from_theme(selected_theme,
-												   (int)x_res.get_value(),
-												   (int)y_res.get_value());
-			// create an EditorWindow for the new Document
-			var editor = new EditorWindow(document);
-
-			Main.add_window(editor);
-			Main.remove_welcome();
-		}
-		catch (Error e)
-		{
-			error_dialog(_("Error creating new document"), e.message);
-		}
+		Main.new_from_theme(selected_theme,
+		                    (int)x_res.get_value(),
+		                    (int)y_res.get_value());
 	}
 	
 	private void set_resolution_box(int width, int height)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]