From: Jean-Philippe Bruyère Date: Thu, 25 Mar 2021 11:39:06 +0000 (+0100) Subject: IEditableTextWidget, cache design FieldInfos for iml X-Git-Tag: v0.9.5-beta~55 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=66e20999196abae2c301baa16cfedd45b9f9b58d;p=jp%2Fcrow.git IEditableTextWidget, cache design FieldInfos for iml --- diff --git a/Crow/src/IML/CompilerServices.cs b/Crow/src/IML/CompilerServices.cs index 1bf378a9..f47f4fb8 100644 --- a/Crow/src/IML/CompilerServices.cs +++ b/Crow/src/IML/CompilerServices.cs @@ -91,6 +91,12 @@ namespace Crow.IML #if DESIGN_MODE internal static MethodInfo miDicStrStrAdd = typeof(Dictionary).GetMethod ("set_Item", new Type[] { typeof(string), typeof(string) }); + internal static FieldInfo fiWidget_design_id = typeof(Widget).GetField("design_id"); + internal static FieldInfo fiWidget_design_line = typeof(Widget).GetField("design_line"); + internal static FieldInfo fiWidget_design_column = typeof(Widget).GetField("design_column"); + internal static FieldInfo fiWidget_design_imlPath = typeof(Widget).GetField("design_imlPath"); + internal static FieldInfo fiWidget_design_iml_values = typeof(Widget).GetField("design_iml_values"); + #endif #region tree handling methods diff --git a/Crow/src/IML/Instantiator.cs b/Crow/src/IML/Instantiator.cs index 34f904b7..f43e700d 100644 --- a/Crow/src/IML/Instantiator.cs +++ b/Crow/src/IML/Instantiator.cs @@ -386,7 +386,7 @@ namespace Crow.IML { void emitSetDesignAttribute (IMLContext ctx, string name, string value){ //store member value in iml ctx.il.Emit (OpCodes.Ldloc_0); - ctx.il.Emit (OpCodes.Ldfld, typeof(Widget).GetField("design_iml_values")); + ctx.il.Emit (OpCodes.Ldfld, CompilerServices.fiWidget_design_iml_values); ctx.il.Emit (OpCodes.Ldstr, name); if (string.IsNullOrEmpty (value)) ctx.il.Emit (OpCodes.Ldnull); @@ -409,17 +409,17 @@ namespace Crow.IML { IXmlLineInfo li = (IXmlLineInfo)reader; ctx.il.Emit (OpCodes.Ldloc_0); ctx.il.Emit (OpCodes.Ldstr, this.NextDesignID); - ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_id")); + ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_id); ctx.il.Emit (OpCodes.Ldloc_0); ctx.il.Emit (OpCodes.Ldc_I4, ctx.curLine + li.LineNumber); - ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_line")); + ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_line); ctx.il.Emit (OpCodes.Ldloc_0); ctx.il.Emit (OpCodes.Ldc_I4, li.LinePosition); - ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_column")); + ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_column); if (!string.IsNullOrEmpty (sourcePath)) { ctx.il.Emit (OpCodes.Ldloc_0); ctx.il.Emit (OpCodes.Ldstr, sourcePath); - ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_imlPath")); + ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_imlPath); } #endif #region Styling and default values loading diff --git a/Crow/src/Interface.cs b/Crow/src/Interface.cs index 4cd9eabb..98df8cee 100644 --- a/Crow/src/Interface.cs +++ b/Crow/src/Interface.cs @@ -1065,11 +1065,11 @@ namespace Crow /// public static long TEXT_CURSOR_BLINK_FREQUENCY = 400; internal Rectangle? textCursor = null;//last printed cursor, used to clear it. - internal bool forceTextCursor = true;//when true, cursor is printed even if blinkingCursor.elapsed is not reached. + public bool forceTextCursor = true;//when true, cursor is printed even if blinkingCursor.elapsed is not reached. Stopwatch blinkingCursor = Stopwatch.StartNew (); void drawTextCursor (Context ctx) { if (forceTextCursor) { - if (FocusedWidget is Label lab) { + if (FocusedWidget is IEditableTextWidget lab) { if (lab.DrawCursor (ctx, out Rectangle c)) { if (textCursor != null && c != textCursor.Value) RegisterClip (textCursor.Value); @@ -1084,7 +1084,7 @@ namespace Crow RegisterClip (textCursor.Value); textCursor = null; blinkingCursor.Restart (); - } else if (FocusedWidget is Label lab && lab.SelectionIsEmpty) { + } else if (FocusedWidget is IEditableTextWidget lab) { if (blinkingCursor.ElapsedMilliseconds > TEXT_CURSOR_BLINK_FREQUENCY) { if (lab.DrawCursor (ctx, out Rectangle c)) { textCursor = c; @@ -1752,44 +1752,40 @@ namespace Crow #endregion #region ILayoutable implementation - public virtual bool PointIsIn (ref Point m) - { - return true; - } + public virtual bool PointIsIn (ref Point m) => true; public void RegisterClip (Rectangle r) { clipping.UnionRectangle (r); } - public bool ArrangeChildren { get { return false; } } + public bool ArrangeChildren => false; public int LayoutingTries { get { throw new NotImplementedException (); } set { throw new NotImplementedException (); } } public LayoutingType RegisteredLayoutings { - get { return LayoutingType.None; } + get => LayoutingType.None; set { throw new NotImplementedException (); } } public void RegisterForLayouting (LayoutingType layoutType) { throw new NotImplementedException (); } public bool UpdateLayout (LayoutingType layoutType) { throw new NotImplementedException (); } - public Rectangle ContextCoordinates (Rectangle r) { return r; } - public Rectangle ScreenCoordinates (Rectangle r) { return r; } + public Rectangle ContextCoordinates (Rectangle r) => r; + public Rectangle ScreenCoordinates (Rectangle r) => r; public ILayoutable Parent { - get { return null; } + get => null; set { throw new NotImplementedException (); } } public ILayoutable LogicalParent { - get { return null; } + get => null; set { throw new NotImplementedException (); } } - public Rectangle ClientRectangle { - get { return clientRectangle; } - } + public Rectangle ClientRectangle => clientRectangle; public Interface HostContainer { get { return this; } } - public Rectangle getSlot () { return ClientRectangle; } + public Rectangle getSlot () => ClientRectangle; + public void ChildrenLayoutingConstraints(ILayoutable layoutable, ref LayoutingType layoutType){ } #endregion } } diff --git a/Crow/src/Text/IEditableTextWidget.cs b/Crow/src/Text/IEditableTextWidget.cs new file mode 100644 index 00000000..20092acb --- /dev/null +++ b/Crow/src/Text/IEditableTextWidget.cs @@ -0,0 +1,22 @@ +using System; +using Crow.Cairo; + +namespace Crow +{ + /// + /// Implement this interface to have a blinking cursor for a widget + /// with editable text ability. + /// + public interface IEditableTextWidget + { + /// + /// Draw text cursor in widget with screen coordinates. This interface when implemented is called + /// automatically by the Interface to make the cursor blink. The blinking frequency is controlled by + /// Interface.TEXT_CURSOR_BLINK_FREQUENCY static field. + /// + /// The master interface context on which to draw the cursor + /// Return a clipping rectangle containing the cursor position to be cleared when needed + /// True if cursor were drawed, false otherwise + bool DrawCursor (Context ctx, out Rectangle rect); + } +} \ No newline at end of file diff --git a/Crow/src/Widgets/Label.cs b/Crow/src/Widgets/Label.cs index 9183f7d8..9d6af8b8 100644 --- a/Crow/src/Widgets/Label.cs +++ b/Crow/src/Widgets/Label.cs @@ -14,7 +14,7 @@ namespace Crow /// /// Simple label widget possibly multiline but without tabulation handling. /// - public class Label : Widget + public class Label : Widget, IEditableTextWidget { #region CTOR protected Label () {} @@ -490,7 +490,11 @@ namespace Crow return null; return cursor; } - internal virtual bool DrawCursor (Context ctx, out Rectangle rect) { + public virtual bool DrawCursor (Context ctx, out Rectangle rect) { + if (CurrentLoc == null || !SelectionIsEmpty) { + rect = default; + return false; + } if (!CurrentLoc.Value.HasVisualX) { ctx.SelectFontFace (Font.Name, Font.Slant, Font.Wheight); ctx.SetFontSize (Font.Size); diff --git a/Crow/src/Widgets/TextBox.cs b/Crow/src/Widgets/TextBox.cs index 1f940896..68bcffca 100644 --- a/Crow/src/Widgets/TextBox.cs +++ b/Crow/src/Widgets/TextBox.cs @@ -210,12 +210,6 @@ namespace Crow return cursor; } - /*internal override bool DrawCursor (Context ctx, out Rectangle rect) { - ctx.Translate (-scrollX, -scrollY); - bool result = base.DrawCursor (ctx, out rect); - ctx.Translate (scrollX, scrollY); - return result; - }*/ void updateMaxScrolls (LayoutingType layout) { Rectangle cb = ClientRectangle;