Keyboard settings work (modifiers untested)

This commit is contained in:
Tamius Han 2017-12-30 12:55:58 +01:00
parent 0f2d786780
commit 82dd71eb5b
7 changed files with 155 additions and 136 deletions

View File

@ -1,7 +1,9 @@
Debug = { Debug = {
debug: true, debug: true,
keyboard: true,
debugResizer: true, debugResizer: true,
debugArDetect: true, debugArDetect: false,
debugStorage: true,
showArDetectCanvas: false, showArDetectCanvas: false,
flushStoredSettings: true flushStoredSettings: true
} }

View File

@ -1,10 +1,10 @@
if(Debug.debug) if(Debug.debug)
console.log("Loading: Keybinds.js"); console.log("Loading: Keybinds.js");
// Yeah hi /r/badcode. var _kbd_ModKeys = ["ctrlKey", "shiftKey", "altKey"];
// Anyway, because nazi localstorage flat out refuses to store arrays: var _kbd_keybinds = {};
var DEFAULT_KEYBINDINGS = [ var DEFAULT_KEYBINDINGS_OLD = [
{ action: "fitw", { action: "fitw",
key: 'w', key: 'w',
modifiers: [] modifiers: []
@ -31,25 +31,19 @@ var DEFAULT_KEYBINDINGS = [
}, },
{ {
action: "char", action: "char",
targetAR: (21/9), targetAR: 2.39,
key: "d", key: "d",
modifiers: [] modifiers: []
}, },
{ {
action: "char", action: "char",
targetAR: (16/9), targetAR: 1.78,
key: "s", key: "s",
modifiers: [] modifiers: []
}, },
{ {
action: "char", action: "char",
targetAR: (16/10), targetAR: 2.0,
key: "x",
modifiers: []
},
{
action: "char",
targetAR: (4/3),
key: "c", key: "c",
modifiers: [] modifiers: []
}, },
@ -60,29 +54,35 @@ var DEFAULT_KEYBINDINGS = [
} }
]; ];
var DEFAULT_KEYBINDINGS = {
// functions "w": {
action: "fitw"
var _kbd_callback = function(keys) { },
if (keys === null || keys === {} || keys === [] || keys == ""){ "e": {
StorageManager.setopt( {"keybinds": DEFAULT_KEYBINDINGS} ); action: "fith"
keys = DEFAULT_KEYBINDINGS; },
"r": {
action: "reset"
},
"a": {
action: "autoar"
},
"s": {
action: "char",
targetAr: 1.78
},
"d": {
action: "char",
targetAr: 2.39
},
"x": {
action: "char",
targetAr: 2.0
} }
_kbd_setup_apply(keys);
}
var _kbd_setup_init = function() {
return StorageManager.getopt("keybinds", _kbd_callback);
} }
var _kbd_process = function (event) { // Tukaj ugotovimo, katero tipko smo pritisnili
var _kbd_setup_apply = function(keybinds){
if(Debug.debug || Debug.keyboard)
console.log("uw::keydownSetup | starting keybord shortcut setup");
$(document).keydown(function (event) { // Tukaj ugotovimo, katero tipko smo pritisnili
// Tipke upoštevamo samo, če smo v celozaslonskem načinu oz. če ne pišemo komentarja // Tipke upoštevamo samo, če smo v celozaslonskem načinu oz. če ne pišemo komentarja
// v nasprotnem primeru ne naredimo nič. // v nasprotnem primeru ne naredimo nič.
@ -91,79 +91,85 @@ var _kbd_setup_apply = function(keybinds){
(document.activeElement.getAttribute("role") != "textbox") && (document.activeElement.getAttribute("role") != "textbox") &&
(document.activeElement.getAttribute("type") != "text") (document.activeElement.getAttribute("type") != "text")
))){ ))){
if(Debug.debug || Debug.keyboard) if(Debug.debug && Debug.keyboard)
console.log("We're writing a comment or something. Doing nothing"); console.log("[Keybinds::_kbd_process] We're writing a comment or something. Doing nothing");
return; return;
} }
if(Debug.debug || Debug.keyboard ){ if(Debug.debug && Debug.keyboard ){
// console.log(keybinds); console.log("[Keybinds::_kbd_process] we pressed a key: ", event.key , " | keydown: ", event.keydown, "event:", event);
console.log("we pressed a key: ", event.key , " | keydown: ", event.keydown); }
if(event.key == 'p'){
console.log("uw/keydown: attempting to send message") // building modifiers list:
var sending = browser.runtime.sendMessage({ var modlist = "";
type: "debug", for(var mod of _kbd_ModKeys){
message: "Test message, please ignore" if(event[mod])
}); modlist += (mod + "_")
sending.then( function(){}, function(){console.log("uw/keydown: there was an error while sending a message")} ); }
console.log("uw/keydown: test message sent! (probably)");
// return; if(Debug.debug && Debug.keyboard ){
if(modlist)
console.log("[Keybinds::_kbd_process] there's been modifier keys. Modlist:", modlist);
}
var keypress = modlist + event.key.toLowerCase();
if(Debug.debug && Debug.keyboard )
console.log("[Keybinds::_kbd_process] our full keypress is this", keypress, "_kbd_keybinds:", {kb: _kbd_keybinds} );
if(_kbd_keybinds[keypress]){
var conf = _kbd_keybinds[keypress];
if(Debug.debug && Debug.keyboard)
console.log("[Keybinds::_kbd_process] there's an action associated with this keypress. conf:", conf);
if(conf.action != "autoar")
ArDetect.stop();
if(conf.action == "char"){
// Status.arStat = "fixed";
Resizer.setAr(conf.targetAr);
}
else{
Resizer.legacyAr(conf.action);
}
} }
} }
for(i in keybinds){
if(Debug.debug || Debug.keyboard)
console.log("i: ", i, "keybinds[i]:", keybinds[i]);
if(event.key == keybinds[i].key){ var _kbd_setup = async function() {
if(Debug.debug || Debug.keyboard)
console.log("Key matches!");
//Tipka se ujema. Preverimo še modifierje:
//Key matches. Let's check if modifiers match, too:
var mods = true;
for(var j = 0; j < keybinds[i].modifiers.length; j++){
if(keybinds[i].modifiers[j] == "ctrl")
mods &= event.ctrlKey ;
else if(keybinds[i].modifiers[j] == "alt")
mods &= event.altKey ;
else if(keybinds[i].modifiers[j] == "shift")
mods &= event.shiftKey ;
}
if(Debug.debug || Debug.keyboard)
console.log("we pressed a key: ", event.key , " | mods match?", mods, "keybinding: ", keybinds[i]);
if(mods){
event.stopPropagation();
console.log("uw::keydown | keys match. Taking action."); if(Debug.debug)
if(keybinds[i].action == "char"){ console.log("[Keybinds::_kbd_setup_init] Setting up keybinds");
Status.arStrat = "fixed";
Status["lastAr"] = keybinds[i].targetAR;
Resizer.setAr(keybinds[i].targetAR);
return;
}
if(keybinds[i].action == "autoar"){
Status.arStrat = "auto";
return;
}
// changeCSS("anything goes", keybinds[i].action);
Status.arStrat = keybinds[i].action;
Resizer.legacyAr(keybinds[i].action);
return;
}
}
}
});
// document.addEventListener("mozfullscreenchange", function( event ) { var ret = await StorageManager.getopt_async("keybinds");
// onFullScreenChange();
// inFullScreen = ( window.innerHeight == window.screen.height && window.innerWidth == window.screen.width); var keybinds = ret.keybinds;
// inFullScreen ? onFullscreenOn() : onFullscreenOff();
// }); if(Array.isArray(keybinds)){
StorageManager.delopt("keybinds");
keybinds = DEFAULT_KEYBINDINGS;
} }
// _kbd_setup_stage2();
// _kbd_setup_init(); if(Debug.debug)
console.log("[Keybinds::_kbd_setup_init] loaded keybinds from storage. Do they exist?", keybinds, $.isEmptyObject(keybinds));
if( $.isEmptyObject(keybinds) ){
keybinds = DEFAULT_KEYBINDINGS;
StorageManager.setopt({"keybinds":keybinds});
if(Debug.debug)
console.log("[Keybinds::_kbd_setup_init] setting keybinds to default", keybinds);
}
_kbd_keybinds = keybinds;
$(document).keydown(_kbd_process);
}
var Keybinds = { var Keybinds = {
init: _kbd_setup_init, init: _kbd_setup
apply: _kbd_setup_apply
} }

View File

@ -13,10 +13,18 @@ var _sm_getopt = function(prop, callback){
var _sm_getopt_async = async function(prop){ var _sm_getopt_async = async function(prop){
if(Debug.debug && Debug.debugStorage)
console.log("[StorageManager::_sm_getopt_async] requesting prop",prop,"from localStorage.");
if(BrowserDetect.usebrowser == "chrome") if(BrowserDetect.usebrowser == "chrome")
return await browser.storage.local.get(prop); return await browser.storage.local.get(prop);
else else{
return await browser.storage.local.get(prop); var ret = await browser.storage.local.get(prop);
if(Debug.debug && Debug.debugStorage)
console.log("[StorageManager::_sm_getopt_async] got prop", prop, "; value: ", ret);
return ret;
}
} }
var _sm_delopt = function(item){ var _sm_delopt = function(item){

View File

@ -428,6 +428,7 @@ var ArDetect = {
_forcehalt: false, _forcehalt: false,
arSetup: _arSetup, arSetup: _arSetup,
init: _arSetup,
vdraw: _ard_vdraw, vdraw: _ard_vdraw,
detectedAr: 1, detectedAr: 1,
arChangedCallback: function() {}, arChangedCallback: function() {},

View File

@ -330,6 +330,20 @@ var setVideoAr = function(aspect_ratio, video, player){
// console.log("uw::setBestFit | css applied"); // console.log("uw::setBestFit | css applied");
// } // }
var _res_reset = function(force){
dimensions = {top: "", left: "", width: "100%", height: "100%"};
$("video").css({"position": "relative", "width": dimensions.width,"height": dimensions.height,"top": dimensions.top, "left": dimensions.left});
if(Debug.debug)
console.log("[Resizer::_res_reset] css applied. Dimensions/pos: w:",dimensions.width,"; h:",dimensions.height,"; top:",dimensions.top,"; left:",dimensions.left);
if(force)
this._currentAr = -1;
}
// Skrbi za "stare" možnosti, kot na primer "na širino zaslona", "na višino zaslona" in "ponastavi". Približevanje opuščeno. // Skrbi za "stare" možnosti, kot na primer "na širino zaslona", "na višino zaslona" in "ponastavi". Približevanje opuščeno.
// handles "legacy" options, such as 'fit to widht', 'fit to height' and 'reset'. No zoom tho // handles "legacy" options, such as 'fit to widht', 'fit to height' and 'reset'. No zoom tho
var _res_legacyAr = function(action){ var _res_legacyAr = function(action){
@ -337,8 +351,6 @@ var _res_legacyAr = function(action){
var ar = screen.width / screen.height; var ar = screen.width / screen.height;
var fileAr = vid.videoWidth / vid.videoHeight; var fileAr = vid.videoWidth / vid.videoHeight;
if(action == "fitw"){ if(action == "fitw"){
_res_setAr_kbd( ar > fileAr ? ar : fileAr); _res_setAr_kbd( ar > fileAr ? ar : fileAr);
return; return;
@ -352,19 +364,9 @@ var _res_legacyAr = function(action){
this.reset(true); this.reset(true);
return; return;
} }
if(action == "autoar"){
ArDetect.init();
} }
var _res_reset = function(force){
dimensions = {top: "", left: "", width: "100%", height: "100%"};
$("video").css({"position": "relative", "width": dimensions.width,"height": dimensions.height,"top": dimensions.top, "left": dimensions.left});
if(Debug.debug)
console.log("[Resizer::_res_reset] css applied. Dimensions/pos: w:",dimensions.width,"; h:",dimensions.height,"; top:",dimensions.top,"; left:",dimensions.left);
if(force)
this._currentAr = -1;
} }
var _res_setAr_kbd = function(ar){ var _res_setAr_kbd = function(ar){

View File

@ -13,7 +13,6 @@ async function main(){
var kbpromise = Keybinds.init(); var kbpromise = Keybinds.init();
ExtensionConf.init(); ExtensionConf.init();
console.log(scpromise);
// počakamo, da so nastavitve naložene // počakamo, da so nastavitve naložene
// wait for settings to load // wait for settings to load

View File

@ -41,6 +41,7 @@
"js/modules/PageInfo.js", "js/modules/PageInfo.js",
"js/modules/ArDetect.js", "js/modules/ArDetect.js",
"js/modules/Resizer.js", "js/modules/Resizer.js",
"js/conf/Keybinds.js", "js/conf/Keybinds.js",
"js/uw.js" ], "js/uw.js" ],