Browse Source

key shortcuts

Nicolas Cannasse 8 years ago
parent
commit
8c0f5dcbec
8 changed files with 104 additions and 31 deletions
  1. 1 0
      bin/defaultProps.json
  2. 7 6
      hide/comp/PropsEditor.hx
  3. 10 8
      hide/ui/Ide.hx
  4. 57 0
      hide/ui/Keys.hx
  5. 6 6
      hide/ui/Props.hx
  6. 1 1
      hide/ui/UndoHistory.hx
  7. 17 3
      hide/ui/View.hx
  8. 5 7
      hide/view/FileView.hx

+ 1 - 0
bin/defaultProps.json

@@ -5,6 +5,7 @@
 	// controls
 	"key.undo" : "Ctrl-Z",
 	"key.redo" : "Ctrl-Y",
+	"key.save" : "Ctrl-S",
 
 	// reserved for internal ide usage
 	"hide" : {}

+ 7 - 6
hide/comp/PropsEditor.hx

@@ -4,11 +4,11 @@ class PropsEditor extends Component {
 
 	public var panel : Element;
 	public var content : Element;
-	public var undo : hide.comp.UndoHistory;
+	public var undo : hide.ui.UndoHistory;
 
 	public function new(root,?undo) {
 		super(root);
-		this.undo = undo == null ? new hide.comp.UndoHistory() : undo;
+		this.undo = undo == null ? new hide.ui.UndoHistory() : undo;
 		var e = new Element("<div class='hide-properties'><div class='content'></div><div class='panel'></div></div>").appendTo(root);
 		content = e.find(".content");
 		panel = e.find(".panel");
@@ -47,7 +47,7 @@ class PropsEditor extends Component {
 			var current : Dynamic = Reflect.field(context, fname);
 			var enumValue : Enum<Dynamic> = null;
 			var tempChange = false;
-			var hadTempChange = false;
+			var beforeTempChange = null;
 
 			switch( f.attr("type") ) {
 			case "checkbox":
@@ -113,14 +113,15 @@ class PropsEditor extends Component {
 				if( f.is("select") ) f.blur();
 
 				if( current == newVal ) {
-					if( tempChange || !hadTempChange )
+					if( tempChange || beforeTempChange == null )
 						return;
-					hadTempChange = false;
+					current = beforeTempChange.value;
+					beforeTempChange = null;
 				}
 
 				if( tempChange ) {
 					tempChange = false;
-					hadTempChange = true;
+					if( beforeTempChange == null ) beforeTempChange = { value : current };
 				}
 				else {
 					undo.change(Field(context, fname, current), function() {

+ 10 - 8
hide/ui/Ide.hx

@@ -1,15 +1,8 @@
 package hide.ui;
-import hide.comp.Props;
 
 class Ide {
 
-	public var props : {
-		global : Props,
-		project : Props,
-		user : Props,
-		current : Props,
-	};
-	public var ideProps(get,never) : HideProps;
+	public var currentProps(get,never) : Props;
 	public var projectDir(get,never) : String;
 	public var resourceDir(get,never) : String;
 	public var initializing(default,null) : Bool;
@@ -17,6 +10,14 @@ class Ide {
 	public var mouseX : Int = 0;
 	public var mouseY : Int = 0;
 
+	var props : {
+		global : Props,
+		project : Props,
+		user : Props,
+		current : Props,
+	};
+	var ideProps(get, never) : Props.HideProps;
+
 	var window : nw.Window;
 
 	var layout : golden.Layout;
@@ -152,6 +153,7 @@ class Ide {
 	}
 
 	function get_ideProps() return props.global.source.hide;
+	function get_currentProps() return props.user;
 
 	public function registerUpdate( updateFun ) {
 		updates.push(updateFun);

+ 57 - 0
hide/ui/Keys.hx

@@ -0,0 +1,57 @@
+package hide.ui;
+
+class Keys {
+
+	var config : Props;
+	var keys = new Map<String,Void->Void>();
+
+	public function new( config : Props ) {
+		this.config = config;
+	}
+
+	public function processEvent( e : js.jquery.Event ) {
+		var active = js.Browser.document.activeElement;
+		if( active != null && active.nodeName == "INPUT" ) return;
+
+		var parts = [];
+		if( e.shiftKey )
+			parts.push("Shift");
+		if( e.ctrlKey )
+			parts.push("Ctrl");
+		if( e.altKey )
+			parts.push("Alt");
+		if( e.keyCode >= 'A'.code && e.keyCode <= 'Z'.code )
+			parts.push(String.fromCharCode(e.keyCode));
+		else if( e.keyCode >= 96 && e.keyCode <= 105 )
+			parts.push(String.fromCharCode('0'.code + e.keyCode - 96));
+		else if( e.keyCode == ' '.code )
+			parts.push("Space");
+		else if( e.keyCode == 13 )
+			parts.push("Enter");
+		else if( e.keyCode == 27 )
+			parts.push("Esc");
+		else if( e.keyCode == 16 || e.keyCode == 17 || e.keyCode == 18 ) {
+			// alt-ctrl-shift
+		} else {
+			//trace(e.key + "=" + e.keyCode+" (" + String.fromCharCode(e.keyCode) + ")");
+			if( e.key != "" )
+				parts.push(e.key);
+			else
+				return;
+		}
+
+		var key = parts.join("-");
+		var callb = keys.get(key);
+		if( callb != null ) callb();
+	}
+
+	public function register( name : String, callb : Void -> Void ) {
+		var key = config.get("key." + name);
+		if( key == null ) {
+			trace("Key not defined " + name);
+			return;
+		}
+		keys.set(key, callb);
+	}
+
+}

+ 6 - 6
hide/comp/Props.hx → hide/ui/Props.hx

@@ -1,4 +1,4 @@
-package hide.comp;
+package hide.ui;
 
 typedef HideProps = {
 	var autoSaveLayout : Null<Bool>;
@@ -18,14 +18,14 @@ typedef PropsDef = {
 
 class Props {
 
-	var ide : hide.ui.Ide;
+	var ide : Ide;
 	var parent : Props;
 	public var path(default,null) : String;
 	public var source(default, null) : PropsDef;
 	public var current : PropsDef;
 
 	public function new( ?parent : Props ) {
-		ide = hide.ui.Ide.inst;
+		ide = Ide.inst;
 		this.parent = parent;
 		sync();
 	}
@@ -139,12 +139,12 @@ class Props {
 			first = false;
 			parts.pop();
 		}
-		var parent = ide.props.user;
+		var parent = ide.currentProps;
 		for( p in propFiles ) {
-			parent = new hide.comp.Props(parent);
+			parent = new Props(parent);
 			parent.load(p);
 		}
-		return allowSave ? parent : new hide.comp.Props(parent);
+		return allowSave ? parent : new Props(parent);
 	}
 
 }

+ 1 - 1
hide/comp/UndoHistory.hx → hide/ui/UndoHistory.hx

@@ -1,4 +1,4 @@
-package hide.comp;
+package hide.ui;
 
 enum HistoryElement {
 	Field( obj : Dynamic, field : String, oldValue : Dynamic );

+ 17 - 3
hide/ui/View.hx

@@ -15,6 +15,9 @@ class View<T> {
 	var ide : Ide;
 	var container : golden.Container;
 	var state : T;
+	var keys(get,null) : Keys;
+	var props(get,null) : Props;
+	var undo = new hide.ui.UndoHistory();
 	public var defaultOptions(get,never) : ViewOptions;
 
 	var contentWidth(get,never) : Int;
@@ -25,6 +28,17 @@ class View<T> {
 		ide = Ide.inst;
 	}
 
+	function get_props() {
+		if( props == null )
+			props = ide.currentProps;
+		return props;
+	}
+
+	function get_keys() {
+		if( keys == null ) keys = new Keys(props);
+		return keys;
+	}
+
 	public function getTitle() {
 		var name = Type.getClassName(Type.getClass(this));
 		return name.split(".").pop();
@@ -53,12 +67,12 @@ class View<T> {
 			}
 			@:privateAccess ide.views.remove(this);
 		});
+		container.getElement().keydown(function(e) {
+			keys.processEvent(e);
+		});
 		untyped cont.parent.__view = this;
 	}
 
-	public function registerKey( name : String, callb : Void -> Void ) {
-	}
-
 	public function rebuild() {
 		if( container == null ) return;
 		syncTitle();

+ 5 - 7
hide/view/FileView.hx

@@ -4,8 +4,6 @@ class FileView extends hide.ui.View<{ path : String }> {
 
 	var extension(get,never) : String;
 	var modified(default,set) : Bool;
-	var props(get, null) : hide.comp.Props;
-	var undo = new hide.comp.UndoHistory();
 
 	function get_extension() {
 		var file = state.path.split("/").pop();
@@ -22,9 +20,9 @@ class FileView extends hide.ui.View<{ path : String }> {
 		undo.onChange = function() {
 			modified = (undo.currentID != lastSave);
 		};
-		registerKey("undo", function() undo.undo());
-		registerKey("redo", function() undo.redo());
-		registerKey("save", function() {
+		keys.register("undo", function() undo.undo());
+		keys.register("redo", function() undo.redo());
+		keys.register("save", function() {
 			save();
 			modified = false;
 			lastSave = undo.currentID;
@@ -40,9 +38,9 @@ class FileView extends hide.ui.View<{ path : String }> {
 		return super.onBeforeClose();
 	}
 
-	function get_props() {
+	override function get_props() {
 		if( props == null )
-			props = hide.comp.Props.loadForFile(ide, state.path);
+			props = hide.ui.Props.loadForFile(ide, state.path);
 		return props;
 	}