Skip to content

Commit

Permalink
workingDir: If creating files is not possible in this directory (for …
Browse files Browse the repository at this point in the history
…example requires admin permissions) this is set to userDir.
  • Loading branch information
Osiris-Team committed Aug 7, 2024
1 parent f2ac228 commit 4ea155f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/main/java/com/osiris/desku/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,39 @@
import java.util.zip.ZipFile;

public class App {
public static String name = "My Todo";

/**
* Can be used to store user-specific data. <br>
* Example on Windows: <br>
* C:\Users\UserName\AppName
*/
public static File userDir = new File(System.getProperty("user.home") + "/" + name);
/**
* Should be the directory in which this application was started. <br>
* Can be used to store information that is not specific to an user. <br>
* If creating files is not possible in this directory (for example requires admin permissions)
* this is set to {@link #userDir}.
*/
public static File workingDir = new File(System.getProperty("user.dir"));
public static String name = "My Todo";
static{
boolean hasWritePerms = false;
try{
File dir = new File(workingDir+"/write-perms-test-dir-"+System.currentTimeMillis());
if(!dir.mkdirs()) throw new Exception();
dir.delete();
hasWritePerms = true;
} catch (Exception e) {
hasWritePerms = false;
}
if(!hasWritePerms){
System.out.println("Updated working dir from "+workingDir+" to "+userDir);
workingDir = userDir; // Update working dir
System.setProperty("user.dir", userDir.getAbsolutePath());
}
}


/**
* Examples: google.com or wikipedia.com or localhost
*/
Expand Down Expand Up @@ -64,12 +90,6 @@ public class App {
public static File htmlDir = new File(workingDir + "/html");
public static File styles = new File(htmlDir + "/global-styles.css");
public static File javascript = new File(htmlDir + "/global-javascript.js");
/**
* Can be used to store user-specific data. <br>
* Example on Windows: <br>
* C:\Users\UserName\AppName
*/
public static File userDir = new File(System.getProperty("user.home") + "/" + name);
public static CopyOnWriteArrayList<Route> routes = new CopyOnWriteArrayList<>();
/**
* The default theme that affects all views.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/osiris/desku/ui/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,17 @@ public THIS setZIndex(ZIndex zIndexEnum) {
return _this;
}

/**
* @param percent for example 0.75 would be 75% of the original size.
* Meaning 0 to 1 is equal to 0% to 100%, you can also go above 100%.
*/
public THIS scale(double percent){
sty("zoom", ""+percent);
sty("-moz-transform", "scale("+percent+")");
sty("-moz-transform-origin", "0 0");
return _this;
}

/**
* Based on https://getbootstrap.com/docs/5.3/layout/z-index/
*/
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/osiris/desku/bugs/AdminWorkingDir.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.osiris.desku.bugs;

import com.osiris.desku.App;
import com.osiris.desku.TApp;
import com.osiris.desku.ui.Component;
import com.osiris.desku.ui.UI;
import com.osiris.desku.ui.display.Text;
import com.osiris.desku.ui.layout.Vertical;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AdminWorkingDir {

@Test
void test() throws Throwable {
System.setProperty("user.dir", "C:\\Program Files");
TApp.testAndAwaitResult((asyncResult) -> {
String txtDefValue = "Hello!";
Text txt = new Text(txtDefValue);

assertEquals(App.workingDir, App.userDir);

return new Vertical()
.add(txt)
.later(__ -> {
try{
while(UI.get().isLoading()) Thread.yield(); // Wait to ensure not the internal value is directly returned
// but instead the value is returned from the frontend HTML value attribute of the component.
} catch (Throwable e) {
asyncResult.refEx.set(e);
} finally {
asyncResult.isDone.set(true);
}
});
});

}
}

0 comments on commit 4ea155f

Please sign in to comment.