# HG changeset patch # User Edouard Tisserant # Date 1586858828 -7200 # Node ID 90f9d9782632a93386e1e3dc32236eaa731c8a21 # Parent 220172cbdcffd92cd5089b048fa231c276f696ae SVGHMI: Add button element to HMI:DropDown, also fix problem with computed box margins. diff -r 220172cbdcff -r 90f9d9782632 svghmi/gen_index_xhtml.xslt --- a/svghmi/gen_index_xhtml.xslt Tue Apr 14 10:56:45 2020 +0200 +++ b/svghmi/gen_index_xhtml.xslt Tue Apr 14 12:07:08 2020 +0200 @@ -822,7 +822,7 @@ - text box + text box button dispatch: function(value) { @@ -833,9 +833,9 @@ init: function() { - this.element.setAttribute("onclick", "hmi_widgets[' + this.button_elt.setAttribute("onclick", "hmi_widgets[' - '].on_click()"); + '].on_button_click()"); this.text_bbox = this.text_elt.getBBox() @@ -845,11 +845,7 @@ tmargin = this.text_bbox.y - this.box_bbox.y; - rmargin = this.box_bbox.width - this.text_bbox.width - lmargin; - - bmargin = this.box_bbox.height - this.text_bbox.height - tmargin; - - this.margins = [lmargin, tmargin, rmargin, bmargin].map(x => Math.max(x,0)); + this.margins = [lmargin, tmargin].map(x => Math.max(x,0)); this.content = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", @@ -867,106 +863,102 @@ on_selection_click: function(selection) { + console.log("selected "+selection); + + this.close(); + this.set_selection(selection); }, - on_click: function() { - - if(this.opened){ - - //this.close(); - - }else{ - - this.open(); + on_button_click: function() { + + this.open(); + + }, + + on_backward_click:function(){ + + this.move(false); + + }, + + on_forward_click:function(){ + + this.move(true); + + }, + + set_selection: function(value) { + + this.text_elt.firstElementChild.textContent = + + (value >= 0 && value < this.content.length) ? + + this.content[value] : "?"+String(value)+"?"; + + }, + + grow_text: function(up_to) { + + let count = 1; + + let txt = this.text_elt; + + let first = txt.firstElementChild; + + let bounds = svg_root.getBoundingClientRect(); + + this.lift = 0; + + while(count < up_to) { + + let next = first.cloneNode(); + + next.removeAttribute("y"); + + next.setAttribute("dy", "1.1em"); + + next.textContent = "..."; + + txt.appendChild(next); + + let rect = txt.getBoundingClientRect(); + + if(rect.bottom > bounds.bottom){ + + let backup = first.getAttribute("dy"); + + first.setAttribute("dy", "-"+String((this.lift+1)*1.1)+"em"); + + rect = txt.getBoundingClientRect(); + + if(rect.top > bounds.top){ + + this.lift += 1; + + } else { + + if(backup) + + first.setAttribute("dy", backup); + + else + + first.removeAttribute("dy"); + + txt.removeChild(next); + + return count; + + } + + } + + count++; } - }, - - on_backward_click:function(){ - - this.move(false); - - }, - - on_forward_click:function(){ - - this.move(true); - - }, - - set_selection: function(value) { - - this.text_elt.firstElementChild.textContent = - - (value >= 0 && value < this.content.length) ? - - this.content[value] : "?"+String(value)+"?"; - - }, - - grow_text: function(up_to) { - - let count = 1; - - let txt = this.text_elt; - - let first = txt.firstElementChild; - - let bounds = svg_root.getBoundingClientRect(); - - this.lift = 0; - - while(count < up_to) { - - let next = first.cloneNode(); - - next.removeAttribute("y"); - - next.setAttribute("dy", "1.1em"); - - next.textContent = "..."; - - txt.appendChild(next); - - let rect = txt.getBoundingClientRect(); - - if(rect.bottom > bounds.bottom){ - - let backup = first.getAttribute("dy"); - - first.setAttribute("dy", "-"+String((this.lift+1)*1.1)+"em"); - - rect = txt.getBoundingClientRect(); - - if(rect.top > bounds.top){ - - this.lift += 1; - - } else { - - if(backup) - - first.setAttribute("dy", backup); - - else - - first.removeAttribute("dy"); - - txt.removeChild(next); - - return count; - - } - - } - - count++; - - } - return count; }, @@ -977,6 +969,8 @@ this.reset_box(); + this.element.appendChild(this.button_elt); + this.opened = false; }, @@ -1107,6 +1101,8 @@ this.adjust_box_to_text(); + this.element.removeChild(this.button_elt); + /* TODO disable interaction with background */ this.opened = true; @@ -1149,7 +1145,7 @@ adjust_box_to_text: function(){ - let [lmargin, tmargin, rmargin, bmargin] = this.margins; + let [lmargin, tmargin] = this.margins; let m = this.text_elt.getBBox(); @@ -1159,9 +1155,9 @@ b.y.baseVal.value = m.y - tmargin; - b.width.baseVal.value = lmargin + m.width + rmargin; - - b.height.baseVal.value = tmargin + m.height + bmargin; + b.width.baseVal.value = 2 * lmargin + m.width; + + b.height.baseVal.value = 2 * tmargin + m.height; }, diff -r 220172cbdcff -r 90f9d9782632 svghmi/widget_dropdown.ysl2 --- a/svghmi/widget_dropdown.ysl2 Tue Apr 14 10:56:45 2020 +0200 +++ b/svghmi/widget_dropdown.ysl2 Tue Apr 14 12:07:08 2020 +0200 @@ -2,20 +2,18 @@ template "widget[@type='DropDown']", mode="widget_defs" { param "hmi_element"; - labels("text box"); + labels("text box button"); || dispatch: function(value) { if(!this.opened) this.set_selection(value); }, init: function() { - this.element.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_click()"); + this.button_elt.setAttribute("onclick", "hmi_widgets['«$hmi_element/@id»'].on_button_click()"); this.text_bbox = this.text_elt.getBBox() this.box_bbox = this.box_elt.getBBox() lmargin = this.text_bbox.x - this.box_bbox.x; tmargin = this.text_bbox.y - this.box_bbox.y; - rmargin = this.box_bbox.width - this.text_bbox.width - lmargin; - bmargin = this.box_bbox.height - this.text_bbox.height - tmargin; - this.margins = [lmargin, tmargin, rmargin, bmargin].map(x => Math.max(x,0)); + this.margins = [lmargin, tmargin].map(x => Math.max(x,0)); this.content = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen"]; //this.content = ["one", "two", "three", "four", "5", "6"]; @@ -24,14 +22,12 @@ this.opened = false; }, on_selection_click: function(selection) { + console.log("selected "+selection); + this.close(); this.set_selection(selection); }, - on_click: function() { - if(this.opened){ - //this.close(); - }else{ - this.open(); - } + on_button_click: function() { + this.open(); }, on_backward_click:function(){ this.move(false); @@ -79,6 +75,7 @@ close: function(){ this.reset_text(); this.reset_box(); + this.element.appendChild(this.button_elt); this.opened = false; }, set_complete_text: function(){ @@ -140,6 +137,7 @@ this.set_partial_text(); } this.adjust_box_to_text(); + this.element.removeChild(this.button_elt); /* TODO disable interaction with background */ this.opened = true; }, @@ -161,13 +159,13 @@ b.height.baseVal.value = m.height; }, adjust_box_to_text: function(){ - let [lmargin, tmargin, rmargin, bmargin] = this.margins; + let [lmargin, tmargin] = this.margins; let m = this.text_elt.getBBox(); let b = this.box_elt; b.x.baseVal.value = m.x - lmargin; b.y.baseVal.value = m.y - tmargin; - b.width.baseVal.value = lmargin + m.width + rmargin; - b.height.baseVal.value = tmargin + m.height + bmargin; + b.width.baseVal.value = 2 * lmargin + m.width; + b.height.baseVal.value = 2 * tmargin + m.height; }, || } diff -r 220172cbdcff -r 90f9d9782632 tests/svghmi/svghmi_0@svghmi/svghmi.svg --- a/tests/svghmi/svghmi_0@svghmi/svghmi.svg Tue Apr 14 10:56:45 2020 +0200 +++ b/tests/svghmi/svghmi_0@svghmi/svghmi.svg Tue Apr 14 12:07:08 2020 +0200 @@ -16,7 +16,7 @@ version="1.1" id="hmi0" sodipodi:docname="svghmi.svg" - inkscape:version="0.92.3 (2405546, 2018-03-11)"> + inkscape:version="0.92.4 (5da689c313, 2019-01-14)"> @@ -124,17 +124,17 @@ inkscape:pageopacity="0" inkscape:pageshadow="2" inkscape:document-units="px" - inkscape:current-layer="g2432" + inkscape:current-layer="g14237" showgrid="false" units="px" inkscape:zoom="0.35355339" inkscape:cx="-418.67433" inkscape:cy="-1206.7258" - inkscape:window-width="1800" - inkscape:window-height="836" - inkscape:window-x="0" - inkscape:window-y="27" - inkscape:window-maximized="1" + inkscape:window-width="2112" + inkscape:window-height="1367" + inkscape:window-x="2960" + inkscape:window-y="54" + inkscape:window-maximized="0" showguides="true" inkscape:guide-bbox="true" /> @@ -2492,33 +2492,52 @@ + inkscape:label="HMI:DropDown:Primes" + transform="translate(0,-640)"> sel_0 + x="881.44226" + sodipodi:role="line" + id="tspan421">sel_0 + + inkscape:label="HMI:ScrollbarTemplate" + transform="translate(0,-640)"> onetwothreefiveseveneleven