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: true,
keyboard: true,
debugResizer: true,
debugArDetect: true,
debugArDetect: false,
debugStorage: true,
showArDetectCanvas: false,
flushStoredSettings: true
}

View File

@ -1,10 +1,10 @@
if(Debug.debug)
console.log("Loading: Keybinds.js");
// Yeah hi /r/badcode.
// Anyway, because nazi localstorage flat out refuses to store arrays:
var _kbd_ModKeys = ["ctrlKey", "shiftKey", "altKey"];
var _kbd_keybinds = {};
var DEFAULT_KEYBINDINGS = [
var DEFAULT_KEYBINDINGS_OLD = [
{ action: "fitw",
key: 'w',
modifiers: []
@ -31,25 +31,19 @@ var DEFAULT_KEYBINDINGS = [
},
{
action: "char",
targetAR: (21/9),
targetAR: 2.39,
key: "d",
modifiers: []
},
{
action: "char",
targetAR: (16/9),
targetAR: 1.78,
key: "s",
modifiers: []
},
{
action: "char",
targetAR: (16/10),
key: "x",
modifiers: []
},
{
action: "char",
targetAR: (4/3),
targetAR: 2.0,
key: "c",
modifiers: []
},
@ -60,110 +54,122 @@ var DEFAULT_KEYBINDINGS = [
}
];
var DEFAULT_KEYBINDINGS = {
"w": {
action: "fitw"
},
"e": {
action: "fith"
},
"r": {
action: "reset"
},
"a": {
action: "autoar"
},
"s": {
action: "char",
targetAr: 1.78
},
"d": {
action: "char",
targetAr: 2.39
},
"x": {
action: "char",
targetAr: 2.0
}
}
// functions
var _kbd_callback = function(keys) {
if (keys === null || keys === {} || keys === [] || keys == ""){
StorageManager.setopt( {"keybinds": DEFAULT_KEYBINDINGS} );
keys = DEFAULT_KEYBINDINGS;
var _kbd_process = function (event) { // Tukaj ugotovimo, katero tipko smo pritisnili
// Tipke upoštevamo samo, če smo v celozaslonskem načinu oz. če ne pišemo komentarja
// v nasprotnem primeru ne naredimo nič.
// We only take actions if we're in full screen or not writing a comment
if( !(FullScreenDetect.isFullScreen() || (
(document.activeElement.getAttribute("role") != "textbox") &&
(document.activeElement.getAttribute("type") != "text")
))){
if(Debug.debug && Debug.keyboard)
console.log("[Keybinds::_kbd_process] We're writing a comment or something. Doing nothing");
return;
}
_kbd_setup_apply(keys);
}
var _kbd_setup_init = function() {
return StorageManager.getopt("keybinds", _kbd_callback);
}
var _kbd_setup_apply = function(keybinds){
if(Debug.debug && Debug.keyboard ){
console.log("[Keybinds::_kbd_process] we pressed a key: ", event.key , " | keydown: ", event.keydown, "event:", event);
}
if(Debug.debug || Debug.keyboard)
console.log("uw::keydownSetup | starting keybord shortcut setup");
$(document).keydown(function (event) { // Tukaj ugotovimo, katero tipko smo pritisnili
// building modifiers list:
var modlist = "";
for(var mod of _kbd_ModKeys){
if(event[mod])
modlist += (mod + "_")
}
// Tipke upoštevamo samo, če smo v celozaslonskem načinu oz. če ne pišemo komentarja
// v nasprotnem primeru ne naredimo nič.
// We only take actions if we're in full screen or not writing a comment
if( !(FullScreenDetect.isFullScreen() || (
(document.activeElement.getAttribute("role") != "textbox") &&
(document.activeElement.getAttribute("type") != "text")
))){
if(Debug.debug || Debug.keyboard)
console.log("We're writing a comment or something. Doing nothing");
return;
}
if(Debug.debug || Debug.keyboard ){
// console.log(keybinds);
console.log("we pressed a key: ", event.key , " | keydown: ", event.keydown);
if(event.key == 'p'){
console.log("uw/keydown: attempting to send message")
var sending = browser.runtime.sendMessage({
type: "debug",
message: "Test message, please ignore"
});
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;
}
}
for(i in keybinds){
if(Debug.debug || Debug.keyboard)
console.log("i: ", i, "keybinds[i]:", keybinds[i]);
if(event.key == keybinds[i].key){
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(keybinds[i].action == "char"){
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;
}
}
}
});
if(Debug.debug && Debug.keyboard ){
if(modlist)
console.log("[Keybinds::_kbd_process] there's been modifier keys. Modlist:", modlist);
}
// document.addEventListener("mozfullscreenchange", function( event ) {
// onFullScreenChange();
// inFullScreen = ( window.innerHeight == window.screen.height && window.innerWidth == window.screen.width);
// inFullScreen ? onFullscreenOn() : onFullscreenOff();
// });
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);
}
}
}
// _kbd_setup_stage2();
// _kbd_setup_init();
var _kbd_setup = async function() {
if(Debug.debug)
console.log("[Keybinds::_kbd_setup_init] Setting up keybinds");
var ret = await StorageManager.getopt_async("keybinds");
var keybinds = ret.keybinds;
if(Array.isArray(keybinds)){
StorageManager.delopt("keybinds");
keybinds = DEFAULT_KEYBINDINGS;
}
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 = {
init: _kbd_setup_init,
apply: _kbd_setup_apply
init: _kbd_setup
}

View File

@ -13,10 +13,18 @@ var _sm_getopt = function(prop, callback){
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")
return await browser.storage.local.get(prop);
else
return await browser.storage.local.get(prop);
else{
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){

View File

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

View File

@ -330,30 +330,7 @@ var setVideoAr = function(aspect_ratio, video, player){
// console.log("uw::setBestFit | css applied");
// }
// 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
var _res_legacyAr = function(action){
var vid = $("video")[0];
var ar = screen.width / screen.height;
var fileAr = vid.videoWidth / vid.videoHeight;
if(action == "fitw"){
_res_setAr_kbd( ar > fileAr ? ar : fileAr);
return;
}
if(action == "fith"){
_res_setAr_kbd( ar < fileAr ? ar : fileAr);
return;
}
if(action == "reset"){
// _res_setAr_kbd(fileAr);
this.reset(true);
return;
}
}
var _res_reset = function(force){
dimensions = {top: "", left: "", width: "100%", height: "100%"};
@ -367,6 +344,31 @@ var _res_reset = function(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.
// handles "legacy" options, such as 'fit to widht', 'fit to height' and 'reset'. No zoom tho
var _res_legacyAr = function(action){
var vid = $("video")[0];
var ar = screen.width / screen.height;
var fileAr = vid.videoWidth / vid.videoHeight;
if(action == "fitw"){
_res_setAr_kbd( ar > fileAr ? ar : fileAr);
return;
}
if(action == "fith"){
_res_setAr_kbd( ar < fileAr ? ar : fileAr);
return;
}
if(action == "reset"){
// _res_setAr_kbd(fileAr);
this.reset(true);
return;
}
if(action == "autoar"){
ArDetect.init();
}
}
var _res_setAr_kbd = function(ar){
if(FullScreenDetect.isFullScreen()){
if(Debug.debug)

View File

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

View File

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