# HG changeset patch # User Edouard Tisserant <edouard.tisserant@gmail.com> # Date 1586898294 -7200 # Node ID ee483e8346f532d497c2956b05518a8b42009f73 # Parent ed3f712a8eeff09ef3bc31f7402e56814d1bcc9e SVGHMI: HMI:DropDown menu aligns to selected when opening diff -r ed3f712a8eef -r ee483e8346f5 svghmi/gen_index_xhtml.xslt --- a/svghmi/gen_index_xhtml.xslt Tue Apr 14 22:08:52 2020 +0200 +++ b/svghmi/gen_index_xhtml.xslt Tue Apr 14 23:04:54 2020 +0200 @@ -905,11 +905,21 @@ </xsl:text> <xsl:text> set_selection: function(value) { </xsl:text> - <xsl:text> this.text_elt.firstElementChild.textContent = -</xsl:text> - <xsl:text> (value >= 0 && value < this.content.length) ? -</xsl:text> - <xsl:text> this.content[value] : "?"+String(value)+"?"; + <xsl:text> let display_str; +</xsl:text> + <xsl:text> if(value >= 0 && value < this.content.length){ +</xsl:text> + <xsl:text> display_str = this.content[value]; +</xsl:text> + <xsl:text> this.last_selection = value; +</xsl:text> + <xsl:text> } else { +</xsl:text> + <xsl:text> display_str = "?"+String(value)+"?"; +</xsl:text> + <xsl:text> } +</xsl:text> + <xsl:text> this.text_elt.firstElementChild.textContent = display_str; </xsl:text> <xsl:text> }, </xsl:text> @@ -1131,6 +1141,18 @@ </xsl:text> <xsl:text> } else { </xsl:text> + <xsl:text> // align to selection +</xsl:text> + <xsl:text> let offset = this.last_selection - this.lift; +</xsl:text> + <xsl:text> if(offset > 0) +</xsl:text> + <xsl:text> this.menu_offset = Math.min(offset + 1, length - slots + 1); +</xsl:text> + <xsl:text> else +</xsl:text> + <xsl:text> this.menu_offset = 0; +</xsl:text> <xsl:text> this.set_partial_text(); </xsl:text> <xsl:text> } diff -r ed3f712a8eef -r ee483e8346f5 svghmi/widget_dropdown.ysl2 --- a/svghmi/widget_dropdown.ysl2 Tue Apr 14 22:08:52 2020 +0200 +++ b/svghmi/widget_dropdown.ysl2 Tue Apr 14 23:04:54 2020 +0200 @@ -41,9 +41,14 @@ this.move(true); }, set_selection: function(value) { - this.text_elt.firstElementChild.textContent = - (value >= 0 && value < this.content.length) ? - this.content[value] : "?"+String(value)+"?"; + let display_str; + if(value >= 0 && value < this.content.length){ + display_str = this.content[value]; + this.last_selection = value; + } else { + display_str = "?"+String(value)+"?"; + } + this.text_elt.firstElementChild.textContent = display_str; }, grow_text: function(up_to) { let count = 1; @@ -150,6 +155,12 @@ if(slots == length) { this.set_complete_text(); } else { + // align to selection + let offset = this.last_selection - this.lift; + if(offset > 0) + this.menu_offset = Math.min(offset + 1, length - slots + 1); + else + this.menu_offset = 0; this.set_partial_text(); } this.adjust_box_to_text();