1 module awebview.gui.contextmenu; 2 3 import awebview.gui.html; 4 import awebview.gui.activity; 5 6 import awebview.gui.widgets.button; 7 8 import std.array, 9 std.format; 10 11 12 13 class ContextMenuListPage : HTMLPage 14 { 15 this(E...)(auto ref E args) 16 { 17 super("_contextmenulist_"); 18 19 foreach(ref e; args){ 20 static if(is(typeof(e) : HTMLElement)) 21 { 22 _args ~= Alg(cast(HTMLElement)e); 23 _elements[e.id] = e; 24 } 25 else 26 _args ~= Alg(e); 27 } 28 29 _elements["_contextmenulist_"] = new IDOnlyElement("_contextmenulist_"); 30 } 31 32 33 override 34 inout(HTMLElement[string]) elements() inout @property 35 { 36 return _elements; 37 } 38 39 40 uint offsetTop(uint idx) 41 { 42 auto jv = this.activity.evalJS(mixin(Lstr!q{ 43 (function(){ 44 var es = document.querySelectorAll("tr"); 45 var elem = es[%[idx%]]; 46 var y = elem.offsetTop; 47 48 while(elem = elem.offsetParent) 49 y += elem.offsetTop; 50 51 return y; 52 }()) 53 })); 54 55 assert(jv.has!uint); 56 return jv.get!uint; 57 } 58 59 60 override 61 @property 62 string html() 63 { 64 bool bBorderedTop; 65 auto app = appender!string(); 66 67 app.put(`<html><head><title>ContextMenuList</title></head><style>tr:hover { background-color: #007bff; color: #000000; }</style>`); 68 app.put(`<body style="margin:0px; padding:0px">`); 69 app.put(`<table id="_contextmenulist_" style="border-collapse: collapse; border: inset 1px black;">`); 70 71 foreach(ref e; _args) 72 { 73 if(e.peek!char) 74 { 75 bBorderedTop = true; 76 continue; 77 } 78 else 79 { 80 if(bBorderedTop) 81 app.put(`<tr><td style="border-top: solid 1px;">`); 82 else 83 app.put(`<tr><td>`); 84 85 if(auto p = e.peek!string) 86 { 87 app.put(*p); 88 } 89 else if(auto p = e.peek!HTMLElement) 90 { 91 app.put(p.html); 92 } 93 94 app.put(`</td></tr>`); 95 bBorderedTop = false; 96 } 97 } 98 99 app.put(`</table></body></html>`); 100 return app.data; 101 } 102 103 104 private: 105 import std.variant : Algebraic; 106 107 alias Alg = Algebraic!(char, string, HTMLElement); 108 109 Alg[] _args; 110 HTMLElement[string] _elements; 111 } 112 113 114 /+ 115 struct ContextMenuItem 116 { 117 static: 118 class DoOnClick : GenericButton!(`<div onclick="%[id%].onClick"></div>`) 119 { 120 this(string id, string text, void delegate() dlg) 121 { 122 super("_contextmenulist_" ~ id, true); 123 this.staticProps["innerHTML"] = text; 124 } 125 } 126 127 128 class ShowOnMouseOver : HTMLElement 129 { 130 this(HTMLPage page) 131 { 132 133 } 134 } 135 } 136 +/