OSDN Git Service

Add Counting Screen-time to KWS and app/screentime [0.2.1]
[kit/kit.git] / system.js
index b2e8ea4..cc8ff3b 100644 (file)
--- a/system.js
+++ b/system.js
@@ -52,7 +52,9 @@ function kit() {
     if( !localStorage.getItem( "kit-appdir" ) ) localStorage.setItem( "kit-appdir", "./app/" );
     S.appdir = localStorage.getItem( "kit-appdir" );
 
-    if( localStorage.getItem( "kit-installed" ) ) System.installed = JSON.parse( localStorage.getItem( "kit-installed" ) );
+    if( localStorage.getItem('kit-installed') ) System.installed = JSON.parse( localStorage.getItem('kit-installed') );
+
+    if( localStorage.getItem('kit-screentime') ) KWS.screenTime = JSON.parse( localStorage.getItem('kit-screentime') );
 
     if( localStorage["kit-userarea"] ) System.userarea = JSON.parse(localStorage["kit-userarea"]);
     if( localStorage["kit-recycle"] ) System.recycle = JSON.parse(localStorage["kit-recycle"]);
@@ -71,7 +73,7 @@ function kit() {
     Notification.push("kitへようこそ", localStorage["kit-username"] + "さん、こんにちは。", "system", null, null, 'documents/icon.png', [
         {
             label: 'kitについて',
-            func: () => launch( 'settings', {'view': 'about'} )
+            func: () => System.launch( 'settings', {'view': 'about'} )
         }
     ]);
 
@@ -83,7 +85,7 @@ function kit() {
         Notification.push( "セーフブート", "現在、kitをセーフモードで起動しています。", "system" );
         System.alert( "セーフブート", "現在、kitをセーフモードで起動しています。<br><a class='kit-hyperlink' onclick='System.reboot()'>通常モードで再起動</a>", "system" );
     }
-    else for( let i of System.startup ) if( i != "" ) launch( i );
+    else for( let i of System.startup ) if( i != "" ) System.launch(i);
     
     $("#kit-header-fullscreen").hide();
 
@@ -122,7 +124,7 @@ function kit() {
             $(".desktop-icons").append("<div class='desktop-icon' data-launch='" + i + "'><img src='" + data[i].icon + "'>" + data[i].name + "</div>");
         }
         $(".desktop-icon").on("click", function(){
-            launch( $(this).attr("data-launch") );
+            System.launch( $(this).attr("data-launch") );
         });
     }).fail( function() {
         Notification.push( "読み込みに失敗", "デスクトップ(config/desktop.json)の読み込みに失敗しました。", "system" );
@@ -257,7 +259,7 @@ function kit() {
                             </div>
                             <div class='--open fa fa-arrow-right'></div>
                         </div>`).appendTo('#kit-sightre-results').on('click', () => {
-                            launch('kish', { 'rc': [ _cmd ] });
+                            System.launch('kish', { 'rc': [ _cmd ] });
                     });
                 }
             }
@@ -270,7 +272,7 @@ function kit() {
                         </div>
                         <div class='--open fa fa-arrow-right'></div>
                     </div>`).appendTo('#kit-sightre-results').on('click', () => {
-                        launch( localStorage.getItem( "kit-default-browser" ), { "url" : _word } );
+                        System.launch( localStorage.getItem('kit-default-browser'), { "url" : _word } );
                 });
             }
             else {
@@ -289,7 +291,7 @@ function kit() {
                         catch(error) {
                             Notification.push("引数の解釈に失敗", error, "system");
                         }
-                        launch( _word.split(",")[0], _args );
+                        System.launch( _word.split(",")[0], _args );
                 });
             }
             for( let i in System.apps ){
@@ -302,7 +304,7 @@ function kit() {
                             </div>
                             <div class='--open fa fa-arrow-right'></div>
                         </div>`).appendTo('#kit-sightre-results').on('click', () => {
-                        launch(i);
+                        System.launch(i);
                         $('#kit-sightre-results').html('');
                         $('#kit-sightre').fadeOut(300);
                     });
@@ -332,7 +334,7 @@ function kit() {
                     </div>
                     <div class='--open fa fa-arrow-right'></div>
                 </div>`).appendTo('#kit-sightre-results').on('click', () => {
-                launch( 'browser', { 'url' : 'https://www.bing.com/search?q=' + _word } );
+                System.launch( 'browser', { 'url' : 'https://www.bing.com/search?q=' + _word } );
                 $('#kit-sightre-results').html('');
                 $('#kit-sightre').fadeOut(300);
             });
@@ -344,7 +346,7 @@ function kit() {
                     </div>
                     <div class='--open fa fa-arrow-right'></div>
                 </div>`).appendTo('#kit-sightre-results').on('click', () => {
-                launch( 'browser', { 'url' : 'https://ja.wikipedia.org/wiki/' + _word } );
+                System.launch( 'browser', { 'url' : 'https://ja.wikipedia.org/wiki/' + _word } );
                 $('#kit-sightre-results').html('');
                 $('#kit-sightre').fadeOut(300);
             });
@@ -377,9 +379,7 @@ function kit() {
         }
     });
 
-    $("#kit-header-user").on("click", ()=>{
-        launch("user");
-    });
+    $('#kit-header-user').on('click', () => System.launch('user') );
 
     $(":root section:not(#desktop-l)").on("contextmenu", function() {
         let _ptelem = $( document.elementFromPoint(S.mouseX, S.mouseY) );
@@ -426,7 +426,7 @@ function kit() {
     });
     $( "#kit-context-search" ).on("click", function(){
         $("#kit-context").fadeOut(300);
-        launch( "browser", { "url" : "https://www.bing.com/search?q=" + $( "#kit-context-input" ).val() } );
+        System.launch( 'browser', {'url': `https://www.bing.com/search?q=${$('#kit-context-input').val()}`} );
     });
     $( "#kit-context-input" ).keypress( function( e ) {
         if( e.which == 13 ) $( "#kit-context-search" ).click();
@@ -451,15 +451,15 @@ function kit() {
         $("#kit-context").fadeOut(300);
     })
 
-    $( document ).delegate( "a", "click", function() {
+    $( document ).delegate('a', 'click', function() {
         if( this.href ) {
-            launch( localStorage.getItem( "kit-default-browser" ), { "url" : this.href } );
+            System.launch( localStorage.getItem( "kit-default-browser" ), { "url" : this.href } );
             return false;
         }
-    } ).on("mousemove", function(event){
-        System.mouseX = event.clientX;
-        System.mouseY = event.clientY;
-    }).delegate( ".textbox", "keypress", function( e ) {
+    } ).on("mousemove", function(e){
+        System.mouseX = e.clientX;
+        System.mouseY = e.clientY;
+    }).delegate('.textbox', 'keypress', function(e) {
         if( e.which == 13 && this.id ){
             if( $("#" + this.id + " + .kit-button").length ){
                 Notification.push("debug", this.id, "system");
@@ -487,14 +487,18 @@ function kit() {
     }
 }
 
-function launch( str, args, dir ) {
+async function launch( str, args, dir ) {
+    while(System.launchLock) await System.ajaxWait();
+    System.launchLock = true;
+
     let _pid = pid;
     System.args[_pid] = args;
-    System.launchpath[_pid] = dir || System.appdir + str;
+    let _path = dir || System.appdir + str;
+    System.launchpath[_pid] = _path;
 
-    if( System.appCache[str] ) {
+    if( System.appCache[_path] ) {
         if( KWS.fullscreen.pid ) KWS.unmax(KWS.fullscreen.pid);
-        appData( System.appCache[str] );
+        appData( System.appCache[_path] );
     }
     else {
         try{
@@ -508,7 +512,7 @@ function launch( str, args, dir ) {
     }
 }
 
-function appData( data ) {
+async function appData( data ) {
     let _pid = pid;
     process[String( _pid )] = {
         id: data.id,
@@ -517,7 +521,7 @@ function appData( data ) {
         preventclose: false,
         title: data.name
     };
-    System.appCache[data.id] = data;
+    System.appCache[System.launchpath[pid]] = data;
     app = new App(_pid);
     let _taskAppend = `<span id='t${_pid}'>`;
     if( data.icon && data.icon != "none" ) _taskAppend += `<img src='${S.launchpath[_pid]}/${data.icon}'>`;
@@ -635,6 +639,7 @@ function appData( data ) {
             $( "head" ).append( '<link href="' + System.launchpath[_pid] + '/' + data.css + '" rel="stylesheet" id="kit-style-' + data.id + '"></link>' );
         }
         localStorage.setItem( "kit-pid", pid );
+        System.launchLock = false;
     } );
 }
 
@@ -690,6 +695,24 @@ const System = new function() {
     this.log = new Array();
     this.noop = () => {}
 
+    this.launchLock = false;
+    
+    this.waitLaunchUnlock = (callback) => {
+        setTimeout(()=>{
+            if(this.ajaxLock){
+                this.waitLaunchUnlock(callback);
+            }else{
+                return callback();
+            }
+        }, 100)
+    }
+
+    this.ajaxWait = () =>{
+        return new Promise(resolve =>{
+             System.waitLaunchUnlock(resolve);
+        });
+    }
+
     this.setBattery = function(){
         if( navigator.getBattery ) navigator.getBattery().then((e)=>{
             let _lv =  e.level * 100;
@@ -722,11 +745,11 @@ const System = new function() {
     }
 
     this.save = function(data, type){
-        launch("fivr", { "save" : data, "type" : type });
+        System.launch("fivr", { "save" : data, "type" : type });
     }
 
     this.open = function(filename){
-        launch("fivr", { "open" : filename });
+        System.launch("fivr", { "open" : filename });
     }
 
     this.preventClose = function( _pid ){
@@ -740,7 +763,7 @@ const System = new function() {
         $( "#kit-power-back" ).click();
         for( let i in process ) {
             if( process[i].preventclose == true ){
-                S.dialog( "シャットダウンの中断", "pid" + System.appCache[process[i].id].name + "がシャットダウンを妨げています。<br>強制終了してシャットダウンを続行する場合は[OK]を押下してください。", () => {
+                S.dialog( "シャットダウンの中断", "pid" + System.appCache[System.launchpath[i]].name + "がシャットダウンを妨げています。<br>強制終了してシャットダウンを続行する場合は[OK]を押下してください。", () => {
                     process[i].preventclose = false;
                     System.shutdown();
                 } );
@@ -773,21 +796,21 @@ const System = new function() {
         else $( "#lock-password" ).hide();
     }
 
-    this.alert = function( title, content, winname ) {
-        launch( "alert", [title, content, winname] );
+    this.alert = function( title, content, wtitle ) {
+        System.launch("alert", [title, content, wtitle]);
     }
 
     this.dialog = function( title, content, func ){
-        launch("dialog", {
+        System.launch("dialog", {
             "title": title,
             "content": content,
             "func": func
-        })
+        });
     }
 
     this.appInfo = function( _pid ){
         let _title = "", _content = "";
-        let ac = System.appCache[process[_pid].id];
+        let ac = System.appCache[S.launchpath[_pid]];
         let _lp = System.launchpath[_pid];
         if( ac ){
             _title = ac.name + " " + ac.version;
@@ -947,22 +970,51 @@ const System = new function() {
         $("#launcher-apps").html("");
         System.apps = data;
         for( let i in data ){
-            $("#launcher-apps").append("<div class='launcher-app' data-launch='" + i + "'><img src='" + data[i].icon + "'>" + data[i].name + "</div>");
+            $('#launcher-apps').append(`<div class='launcher-app' data-launch='${i}'><img src='${data[i].icon}'>${data[i].name}</div>`);
         }
-        if( !System.bootopt.get("safe") ){
+        if( !System.bootopt.get('safe') ){
             for( let i of System.installed ){
-                $("#launcher-apps").append("<div class='launcher-app' data-define-path='" + i.path + "' data-define-id='" + i.id + "'><img src='" + i.icon + "'>" + i.name + "</div>");
+                $('#launcher-apps').append(`<div class='launcher-app' data-launch='${i.path}' data-define-id='${i.id}'><img src='${i.icon}'>${i.name}</div>`);
             }
         }
-        $(".launcher-app").on("click", function(){
-            $("#launch").click();
-            if( $(this).attr("data-launch") ) launch( $(this).attr("data-launch") );
-            else if( $(this).attr("data-define-path") ){
-                launch( $(this).attr("data-define-id"), null, $(this).attr("data-define-path") );
-            };
+        $('.launcher-app').on('click', function(){
+            $('#launch').click();
+            System.launch( $(this).attr('data-launch') );
         });
     }
 
+    this.launch = async function(path, args) {
+        while(this.launchLock) await this.ajaxWait();
+        this.launchLock = true;
+
+        let _pid = pid;
+        System.args[_pid] = args;
+        let _path = path;
+        if(_path.lastIndexOf('/') == _path.length - 1) {
+            _path = _path.substring(0, _path.length - 1);
+        }
+        else if(_path.indexOf('/') == -1) {
+            _path = System.appdir + _path;
+        }
+
+        System.launchpath[_pid] = _path;
+    
+        if( System.appCache[_path] ) {
+            if( KWS.fullscreen.pid ) KWS.unmax(KWS.fullscreen.pid);
+            appData( System.appCache[_path] );
+        }
+        else {
+            try{
+                $.getJSON( _path + '/define.json', appData ).fail( () => {
+                    Notification.push('kitアプリをロードできません。', `${_path}を展開できませんでした。`, 'system');
+                } );
+            }
+            catch(error){
+                Notification.push( "System Error", error, "system" );
+            }
+        }
+    }
+
     this.clip = new function(){
         this.content = null;
         this.history = new Array();
@@ -1021,7 +1073,7 @@ const System = new function() {
 }
 
 const KWS = new function(){
-    this.version = "3.2.2";
+    this.version = "3.2.3";
     this.active = null;
 
     this.darkmode = false;
@@ -1054,10 +1106,16 @@ const KWS = new function(){
     }
 
     this.max = function( _pid ){
-        if( KWS.fullscreen.pid ){
-            Notification.push("最大化に失敗", "最大化しているウィンドウがあります。", "system");
+        let _appcache = System.appCache[System.launchpath[_pid]];
+        if( KWS.fullscreen.pid || _appcache.support.fullscreen != true ){
+            Notification.push('最大化に失敗', 'ウィンドウの最大化に失敗しました。', 'system');
             return;
         }
+        KWS.fullscreen.prevWidth = $("#winc"+_pid).outerWidth();
+        KWS.fullscreen.prevHeight = $("#winc"+_pid).outerHeight();
+        KWS.fullscreen.prevTop = $("#w"+_pid).offset().top;
+        KWS.fullscreen.prevLeft = $("#w"+_pid).offset().left;
+        KWS.fullscreen.pid = _pid;
         $( "#wt"+_pid ).addClass("wtmaximize");
         $( "#w"+_pid ).css({
             "top": "0px",
@@ -1066,18 +1124,9 @@ const KWS = new function(){
         .addClass("windowmaximize")
         .css("z-index", KWS.windowIndex + 1);
         KWS.refreshWindowIndex();
-
-        KWS.fullscreen.prevWidth = $("#winc"+_pid).outerWidth();
-        KWS.fullscreen.prevHeight = $("#winc"+_pid).outerHeight();
-        KWS.fullscreen.prevTop = $("#w"+_pid).offset().top;
-        KWS.fullscreen.prevLeft = $("#w"+_pid).offset().left;
-
         KWS.resize( _pid, System.display.width, System.display.height - 30 );
         $("footer").hide();
-        $("#kit-header-fullscreen").show().on("click", () => {
-            KWS.unmax( _pid );
-        });
-        KWS.fullscreen.pid = _pid;
+        $("#kit-header-fullscreen").show().on('click', () => KWS.unmax( _pid ));
     }
 
     this.unmax = function( _pid ){
@@ -1085,8 +1134,8 @@ const KWS = new function(){
             Notification.push("最大化解除に失敗", "対象がフルスクリーンウィンドウではありません。");
             return;
         }
-        $( "#wt"+_pid ).removeClass("wtmaximize");
-        $( "#w"+_pid ).css({
+        $('#wt'+_pid).removeClass("wtmaximize");
+        $('#w'+_pid).css({
             "top": KWS.fullscreen.prevTop,
             "left": KWS.fullscreen.prevLeft
         })
@@ -1099,6 +1148,10 @@ const KWS = new function(){
         KWS.fullscreen.prevHeight = null;
         KWS.fullscreen.prevTop = null;
         KWS.fullscreen.prevLeft = null;
+
+        if( !System.appCache[System.launchpath[_pid]].size.height ) {
+            System.qs(_pid)[0].style.height = "auto";
+        }
     }
 
     this.vacuum = function( _left, _top ){
@@ -1126,16 +1179,26 @@ const KWS = new function(){
         } );
         for( let i in array ){
             document.getElementById(array[i].id).style.zIndex = i;
+            let _pid = String(array[i].id).substring(1);
             if( i == num-1 ){
                 $("#"+array[i].id).addClass("windowactive");
-                $("#t"+String(array[i].id).substring(1)).addClass("t-active");
-                KWS.active = String(array[i].id).substring(1);
-                process[array[i].id.substring(1)].isactive = true;
+                $("#t"+_pid).addClass("t-active");
+                KWS.active = _pid;
+                process[_pid].isactive = true;
+                KWS.screenPrevSwitched = new Date();
+                localStorage.setItem('kit-screentime', JSON.stringify(KWS.screenTime));
             }
             else{
+                console.log(_pid + '外れました');
                 $("#"+array[i].id).removeClass("windowactive");
-                $("#t"+String(array[i].id).substring(1)).removeClass("t-active");
-                process[array[i].id.substring(1)].isactive = false;
+                $("#t"+_pid).removeClass("t-active");
+                process[_pid].isactive = false;
+                if( KWS.active == _pid ){
+                    let _diff = (new Date() - KWS.screenPrevSwitched);
+                    let _appid = process[_pid].id
+                    if( !KWS.screenTime[_appid] ) KWS.screenTime[_appid] = new Number();
+                    KWS.screenTime[_appid] += _diff;
+                }
             }
         }
         KWS.windowIndex = num;
@@ -1151,6 +1214,17 @@ const KWS = new function(){
         if( _height ) $("#winc"+_pid).css("height", _height);
     }
 
+    this.setTheme = function(_name){
+        localStorage.setItem('kit-theme', _name);
+        if(_name != 'none') $('#kit-theme-file').attr('href', `./system/theme/${localStorage.getItem('kit-theme')}`);
+        else $('#kit-theme-file').attr('href', '');
+        System.moveDesktop(currentDesktop);
+    }
+
+    this.screenTime = new Object();
+
+    this.screenPrevSwitched = new Date();
+
     this.fusen = new function(){
         this.fid = 0;
         this.list = new Object();
@@ -1239,8 +1313,8 @@ const Notification = new function() {
             },
             img: _img
         };
-        if( _pid && System.appCache[_app] ){
-            _app = `<img src='${System.launchpath[_pid]}/${System.appCache[_app].icon}'>${_app}`;
+        if( _pid && System.appCache[System.launchpath[_pid]] ){
+            _app = `<img src='${System.launchpath[_pid]}/${System.appCache[System.launchpath[_pid]].icon}'>${System.appCache[System.launchpath[_pid]].name}`;
         }
         if( !this.goodnight ){
             if( this.sound ) System.audio.play( "n" + this.nid, this.sound );
@@ -1290,7 +1364,7 @@ class App {
         App.d[_pid] = new Object();
         
         this.process = process[_pid];
-        this.info = System.appCache[process[_pid].id];
+        this.cache = System.appCache[System.launchpath[_pid]];
 
         this.args = System.args[_pid];
         this.close = () => System.close(_pid);
@@ -1336,11 +1410,13 @@ class App {
 
     static event( _pid, _name, _event ) {
         if( !App.e[_pid] ) App.e[_pid] = new Object();
-        App.e[_pid][_name] = _event;
+        if( !_event && App.e[_pid][_name] ) App.e[_pid][_name].call();
+        else App.e[_pid][_name] = _event;
         return App;
     }
 
     static getPath( _pid, _path ) {
+        if( String(_path)[0] != '/' ) _path = '/' + _path;
         return System.launchpath[_pid] + _path;
     }
 
@@ -1380,10 +1456,10 @@ class App {
                 $(i).attr("src", `${System.launchpath[_pid]}/${i.getAttribute("kit-src")}` );
             }
             if( i.hasAttribute("kit-alert") ){
-                $(i).on("click", ()=> System.alert( System.appCache[ process[_pid].id ].name, i.getAttribute("kit-alert") ) );
+                $(i).on("click", ()=> System.alert( System.appCache[System.launchpath[_pid]].name, i.getAttribute("kit-alert") ) );
             }
             if( i.hasAttribute("kit-launch") ){
-                $(i).on("click", ()=> launch( i.getAttribute("kit-launch") ) );
+                $(i).on("click", ()=> System.launch( i.getAttribute("kit-launch") ) );
             }
             if( i.hasAttribute("kit-close") ){
                 $(i).on("click", ()=> System.close( i.getAttribute("kit-close") || _pid ) );
@@ -1432,8 +1508,14 @@ class App {
     }
 
     static load( _pid, _path ) {
-        S.dom(_pid).load( System.launchpath[_pid] +"/"+ _path, () => {
+        if( String(_path)[0] != '/' ) _path = '/' + _path;
+        _path = System.launchpath[_pid] + _path;
+        S.dom(_pid).load( _path, () => {
             App.kaf(_pid);
+            let _appcache = System.appCache[System.launchpath[_pid]];
+            if( !KWS.fullscreen.pid && !_appcache.size.height ) {
+                System.qs(_pid)[0].style.height = "auto";
+            }
         } );
         return App;
     }