{"id":1485,"date":"2022-10-23T17:10:00","date_gmt":"2022-10-23T23:10:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1485"},"modified":"2022-10-22T18:14:17","modified_gmt":"2022-10-23T00:14:17","slug":"lions-and-timers-and-such-oh-my","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2022\/10\/23\/lions-and-timers-and-such-oh-my\/","title":{"rendered":"Lions and Timers and Such ! Oh My !"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Anyone following along that has poked around what exists for the <a href=\"https:\/\/bitbucket.org\/einhugur\/einhugur-forms\/src\/master\/\">Einhugur C# UI Framework<\/a> should notice that there IS a Timer object in there already.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since its in the Einhugur.Threading namespace we need to add that using clause to our project<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.Data;\nusing System.Diagnostics;\nusing AppKit;\nusing CloudKit;\nusing Einhugur.Forms;\nusing Einhugur.Threading; \/\/ &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;\n\nusing Mono.Data.Sqlite;\nusing Npgsql;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using one is as simple as declaring the property for it, creating it, setting its <em>period<\/em>, or interval, and adding a method for it to call whenever the Timer needs to run some action.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            timer = new Timer();\n            timer.Interval = 0.05;\n            timer.Action += timer_Action;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One thing you dont need to do, since its not a visual control, is add it to the list of controls in SetUpControls. Since its actually a property on the layout thats enough for it to function exactly as we want.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However a timer by itself isn&#8217;t very interesting. Let&#8217;s have it drive something visual like a Level Indicator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    TextField textfield1;\n    TextField textfield2;\n    ListBox list;\n    Button pushButton;\n    Timer timer;\n    LevelIndicator indicator;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll want to add one more using clause to access the Einhugur Drawing namespace where things like color and gb functions are defined<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Einhugur.Drawing;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And then in SetupControls we need to add code to set the indicator up<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>indicator = new LevelIndicator((float)(pushButton.Left + pushButton.Width + 12), (float)pushButton.Top, 200, 22);\nindicator.Style = LevelIndicator.LevelIndicatorStyle.DiscreteCapacity;\nindicator.CriticalFillColor = Color.RGB(255, 0, 0);\nindicator.WarningFillColor = Color.RGB(0, 0, 255);\nindicator.FillColor = Color.RGB(0, 0, 0);\nindicator.MinValue = 0;\nindicator.MaxValue = 100;\nindicator.WarningValue = indicator.MinValue + (0.50 * (indicator.MaxValue - indicator.MinValue));\nindicator.CriticalValue = indicator.MinValue + (0.90 * (indicator.MaxValue - indicator.MinValue));\nindicator.Value = 0;\n\nreturn new Control&#91;] { textfield1, textfield2, \n                         list, pushButton, indicator };\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Indicators come in several styles. They are explained <a href=\"https:\/\/developer.apple.com\/documentation\/appkit\/nslevelindicatorstyle?language=objc\">on this Apple page<\/a>. But they are easy to play with. One gives you a segmented capacity, like an audio meter, one a continuous bar that has not marked gradation, one shows as a star rating. Try them all \ud83d\ude42<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Certain settings, like critical fill color warning fill color, warning value and critical value may not make sense with some styles. <br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you use the discrete and continue capacity styles they do. And when the value of the level indicator is above the warning, and less than critical that portion of the indicator in the warning color. Same for critical. We&#8217;ll set all these up and hook it to a timer that will reveal what I mean.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the settings above now all we need is the timer action method to do something to the level indicator. Lets make it count up and down to animate the level indicator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> private int increment = 1;\n private void timer_Action(object sender, EventArgs a)\n {\n   if (indicator.Value &lt;= indicator.MinValue)\n   {\n      \/\/ reverse and go up \n      increment = 1;\n   }\n   else if (indicator.Value >= indicator.MaxValue)\n   {\n      \/\/ reverse &amp; go down\n      increment = -1;\n   }\n  \n   \/\/ change the level indicator\n   indicator.Value = indicator.Value + increment;\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">OK we have most everything set up. Instead of hooking this timer up to start when we push the button on our layout, which would be simple, lets start it when the window opens.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We dont want to put the call to the timers start method in the Setup of the controls. So how do we get this into what would be the equivalent of the <em>Open<\/em> event ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you right click on &#8220;Window&#8221; in our definition of the class<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tpublic class MainWindow : Window<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and select &#8220;Go To Definition&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"365\" height=\"508\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.01.09-PM.png?resize=365%2C508&#038;ssl=1\" alt=\"\" class=\"wp-image-1486\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.01.09-PM.png?w=365&amp;ssl=1 365w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.01.09-PM.png?resize=216%2C300&amp;ssl=1 216w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">you get the assembly browser. In there you can see that a Window has an Opening method we can make use of. Perfect !<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"686\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.03.23-PM.png?resize=525%2C686&#038;ssl=1\" alt=\"\" class=\"wp-image-1487\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.03.23-PM.png?w=635&amp;ssl=1 635w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.03.23-PM.png?resize=230%2C300&amp;ssl=1 230w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"541\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.03.37-PM.png?resize=525%2C541&#038;ssl=1\" alt=\"\" class=\"wp-image-1488\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.03.37-PM.png?w=977&amp;ssl=1 977w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.03.37-PM.png?resize=291%2C300&amp;ssl=1 291w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Shot-2022-10-22-at-6.03.37-PM.png?resize=768%2C791&amp;ssl=1 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To override the default implementation we need to tell the compiler this IS an override<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> protected override void Opening()\n {\n    timer.Start();\n }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Often the easiest way to get the right definition for the method you want to override is to copy the declaration from the assembly browser, paste it into your code and change <em>virtual<\/em> to <em>override<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"706\" style=\"aspect-ratio: 1589 \/ 706;\" width=\"1589\" controls src=\"https:\/\/www.great-white-software.com\/blog\/wp-content\/uploads\/2022\/10\/Screen-Recording-2022-10-22-at-6.08.40-PM.mov\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And now as the timer runs its action event it affects the value of the level indicator. On the way up as the level indicator gets to the warning value the color changes  to the warning fill color. And when it gets to the critical value it again changes. And as it goes back down in value the colors change again.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again &#8211; our <a href=\"https:\/\/great-white-software.com\/miscellaneous\/CSharp\/getting_started-2022-10-23.zip\">project as it is now<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anyone following along that has poked around what exists for the Einhugur C# UI Framework should notice that there IS a Timer object in there already. Since its in the Einhugur.Threading namespace we need to add that using clause to our project Using one is as simple as declaring the property for it, creating it, &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2022\/10\/23\/lions-and-timers-and-such-oh-my\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Lions and Timers and Such ! Oh My !&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[34,4],"class_list":["post-1485","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-csharp","tag-macos"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=1485"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1485\/revisions"}],"predecessor-version":[{"id":1490,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1485\/revisions\/1490"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}