Skip to content

Instantly share code, notes, and snippets.

@aferust
Last active August 19, 2020 10:52
Show Gist options
  • Save aferust/648ca4626f6a82c1176a455c78061047 to your computer and use it in GitHub Desktop.
Save aferust/648ca4626f6a82c1176a455c78061047 to your computer and use it in GitHub Desktop.
module app;
import std.conv;
import std.process;
import beamui;
mixin RegisterPlatforms;
int main()
{
// initialize library
GuiApp app;
if (!app.initialize())
return -1;
// load a better theme
platform.stylesheets = [StyleResource("dark")];
// set some global styles before we start
setStyleSheet(currentTheme, css);
// create a window with 1x1 size and expand it to the size of content
Window window = platform.createWindow("Do it later", null, WindowOptions.expanded, 250, 250);
// show it with the temperature converter as its main widget
window.show(() => render!RootWidget);
// run application event loop
return platform.runEventLoop();
}
const css = `
RootWidget {
display: grid;
padding: 6px;
}
.error { border-color: red }
`;
ulong timerId;
class RootWidget : Panel {
static class State : WidgetState
{
version(Windows) {
dstring cmdText = "shutdown /s"d;
}
version(linux) {
dstring cmdText = "shutdown -h now"d;
}
bool isWorking = false;
}
override State createState()
{
return new State;
}
override void build()
{
State st = use!State;
auto spin1 = render!(SpinCtrl!(0, 365, 1, 0));
auto spin2 = render!(SpinCtrl!(0, 23, 1, 0));
auto spin3 = render!(SpinCtrl!(0, 59, 1, 2));
auto spin4 = render!(SpinCtrl!(0, 59, 1, 0));
auto cmdEdit = render!TextField;
cmdEdit.text = st.cmdText;
auto startBut = render!Button;
startBut.text = "start";
auto stopBut = render!Button;
stopBut.text = "stop/pause";
stopBut.onClick = (st.isWorking)?() {
stop(st);
}:null;
startBut.onClick = (!st.isWorking)?(){
setState(st.isWorking, true);
timerId = window.setTimer(1000, (){
int day = spin1.value;
int hour = spin2.value;
int minute = spin3.value;
int second = spin4.value;
if((day == 0) && (hour == 0) && (minute == 0) && (second == 0)){
const ls = executeShell(st.cmdText.to!string);
stop(st);
return false;
} else {
spin4.value = --second;
if(second == -1){
spin4.value = 59;
spin3.value = --minute;
if(minute == -1){
spin3.value = 59;
spin2.value = --hour;
if(hour == -1){
spin2.value = 23;
spin1.value = --day;
}
}
}
}
return true;
});
}:null;
auto resetBut = render!Button;
resetBut.text = "Reset"d;
resetBut.onClick = !st.isWorking?{
spin1.value = 0;
spin2.value = 0;
spin3.value = 0;
spin4.value = 0;
}:null;
wrap(
render((Panel p){
InlineStyle bst;
bst.display = "row";
p.style = bst;
p.wrap(render((GroupBox l){l.caption = "days"; l.wrap(spin1);}),
render((GroupBox l){l.caption = "hours"; l.wrap(spin2);}),
render((GroupBox l){l.caption = "minutes"; l.wrap(spin3);}),
render((GroupBox l){l.caption = "seconds"; l.wrap(spin4);}));
}),
cmdEdit,
render((Panel p){
InlineStyle bst;
bst.display = "row";
p.style = bst;
p.wrap(startBut, stopBut, resetBut);
})
);
}
void stop(ST)(ref ST st){
setState(st.isWorking, false);
window.cancelTimer(timerId);
}
}
class SpinCtrl(int min, int max, int increment, int ival) : Panel
{
class State : WidgetState
{
int value = ival;
}
override State createState()
{
return new State;
}
@property auto value()
{
return use!State.value;
}
@property void value(int val)
{
setState(use!State.value, val);
}
override void build()
{
State st = use!State;
TextField ed1 = render!TextField;
ed1.text = to!dstring(st.value);
onWheelEvent = (WheelEvent event) {
if(enabled){
if((event.deltaY < 0) && (value != max))
value = .min(value + increment, max);
if((event.deltaY > 0) && value != min)
value = .max(value - increment, min);
return true;
}
return false;
};
Button butUp = render!Button;
Button butDown = render!Button;
butUp.text = "+";
butDown.text = "-";
butUp.onClick = () {
setState(st.value, (st.value<max)?st.value + increment : max);
};
butDown.onClick = () {
setState(st.value, (st.value>min)?st.value - increment : min);
};
InlineStyle spinStyle;
spinStyle.display = "row";
style(spinStyle);
InlineStyle ed1Style;
ed1Style.maxHeight = Length.percent(100.0f);
ed1.style(ed1Style);
InlineStyle butStyle;
butStyle.maxHeight = Length.percent(50.0f);
butUp.style(butStyle);
butDown.style(butStyle);
InlineStyle colStyle;
colStyle.display = "column";
Panel col = render!Panel;
col.style(colStyle);
col.wrap(butUp, butDown);
wrap(
render((Panel p){
InlineStyle pStyle;
pStyle.display = "row";
pStyle.maxHeight = Length.px(25);
p.style = pStyle;
p.wrap(ed1, col);
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment