From fce18f2ebe3dd22d817356594ee08dcd83cc3490 Mon Sep 17 00:00:00 2001 From: Xavi Artigas Date: Mon, 17 Sep 2018 14:18:06 -0300 Subject: [PATCH] csharp: Refactor UI examples Summary: Adapt these two UI examples to the style of the Hello World tutorials, using initialization methods, more comments, and try to avoid creating vars for widgets. Reviewers: vitor.sousa, lauromoura Reviewed By: vitor.sousa Differential Revision: https://phab.enlightenment.org/D7038 --- reference/csharp/ui/src/ui_container.cs | 166 +++++++++++++----------- reference/csharp/ui/src/ui_sizing.cs | 63 ++++----- 2 files changed, 125 insertions(+), 104 deletions(-) diff --git a/reference/csharp/ui/src/ui_container.cs b/reference/csharp/ui/src/ui_container.cs index 714572d3..fe5cc976 100644 --- a/reference/csharp/ui/src/ui_container.cs +++ b/reference/csharp/ui/src/ui_container.cs @@ -1,112 +1,128 @@ /* - * Efl.UI container exmaples. + * Efl.UI container examples. * * Load and pack a selection of containers. - * Each has it's own unique layout and methods which are demonstrated below. + * Each has its own unique layout and methods which are demonstrated below. */ using System; public class Example { - // quit the app, called if the user clicks the Quit button or the window is hidden - static void GuiQuitCb(object sender, EventArgs e) + // Create a box container full of buttons + static efl.ui.IBox CreateBox(efl.ui.IWin win) { - efl.ui.Config.Exit(); - } - - // Load a simple table layout into the window - static efl.ui.ITable SetupUiTable(efl.ui.IWin win) - { - efl.ui.ITable table = new efl.ui.Table(win); - - table.SetTableColumns(2); - table.SetTableDirection(efl.ui.Dir.Right, efl.ui.Dir.Down); - - efl.ui.IButton button = new efl.ui.Button(win); - button.SetText("Long Button"); - - table.PackTable(button, 0, 2, 2, 1); + efl.ui.IBox box = new efl.ui.Box(win, (efl.ui.IBox ebox) => { + // Set distance between contained elements + ebox.SetPackPadding(5, 0, true); + }); for (int i = 1; i <= 4; ++i) { - efl.ui.IButton btn = new efl.ui.Button(win); - btn.SetText($"Table {i}"); - table.DoPack(btn); - } - - return table; - } - - // Load some boxes - a horizontal one for the window layout and a vertical - // one to contain a flow - static efl.ui.IBox SetupUiBoxes(efl.ui.IWin win) - { - efl.ui.IBox box = new efl.ui.Box(win); - box.SetPackPadding(5, 0, true); - - for (int i = 1; i <= 4; ++i) - { - efl.ui.IButton button = new efl.ui.Button(win); - button.SetText($"Boxed {i}"); - if (i == 2) - { - eina.Size2D sz; - sz.W = 100; - sz.H = 50; - button.SetHintMax(sz); - } - box.DoPack(button); + // Add 4 buttons, one below the other + new efl.ui.Button(win, (efl.ui.IButton ebutton) => { + ebutton.SetText($"Boxed {i}"); + if (i == 2) + { + // Button 2 has its maximum size limited, so it will be smaller + ebutton.SetHintMax(new eina.Size2D(100,50)); + } + box.DoPack(ebutton); + }); } return box; } + // Create a simple table layout + static efl.ui.ITable CreateTable(efl.ui.IWin win) + { + efl.ui.ITable table = new efl.ui.Table(win, (efl.ui.ITable etable) => { + // Table with two columns, that get filled left to right, and then top to bottom + etable.SetTableColumns(2); + etable.SetTableDirection(efl.ui.Dir.Right, efl.ui.Dir.Down); + }); + + for (int i = 1; i <= 4; ++i) + { + // Add 4 buttons, following the defined table flow + new efl.ui.Button(win, (efl.ui.IButton ebutton) => { + ebutton.SetText($"Table {i}"); + table.DoPack(ebutton); + }); + } + + // Last button spans two table cells + new efl.ui.Button(win, (efl.ui.IButton ebutton) => { + ebutton.SetText("Long Button"); + table.PackTable(ebutton, 0, 2, 2, 1); + }); + + return table; + } + +#if WIN32 + [STAThreadAttribute()] +#endif public static void Main() { + // Initialize EFL and all UI components efl.All.Init(efl.Components.Ui); + // Create a window and initialize it efl.ui.IWin win = new efl.ui.Win(null, (efl.ui.IWin ewin) => { ewin.SetWinType(efl.ui.Win_Type.Basic); - ewin.SetText("Hello World"); + ewin.SetText("Container demo"); ewin.SetAutohide(true); + ewin.HideEvt += (object sender, EventArgs e) => { + // Exit the EFL main loop + efl.ui.Config.Exit(); + }; }); - eina.Size2D sz; - sz.W = 350; - sz.H = 250; - win.SetSize(sz); + // Give the window an initial size so there is room to resize the panes. + // Otherwise, all widgets are tightly packed + win.SetSize(new eina.Size2D(350,250)); - // when the user clicks "close" on a window there is a request to hide - win.HideEvt += GuiQuitCb; + // Create a vertically-split panes container + efl.ui.IPanes vsplit = new efl.ui.Panes(win, (efl.ui.IPanes epanes) => { + epanes.SetSplitRatio(0.75); + win.SetContent(epanes); + }); - // Load a vertical and horizontal split into the window + // Create some boxes and set them as the content of the first pane of the container + var box = CreateBox(win); + efl.Content.static_cast(vsplit.GetPart("first")).SetContent(box); - efl.ui.IPanes split = new efl.ui.Panes(win); - split.SetSplitRatio(0.75); - win.SetContent(split); + // Create a second, horizontally-split panes container and set it as the content of + // the second pane of the first container + efl.ui.IPanes hsplit = new efl.ui.Panes(win, (efl.ui.IPanes epanes) => { + epanes.SetDirection(efl.ui.Dir.Horizontal); + epanes.SetSplitRatio(0.85); + }); + efl.Content.static_cast(vsplit.GetPart("second")).SetContent(hsplit); - var boxes = SetupUiBoxes(win); - efl.Content.static_cast(split.GetPart("first")).SetContent(boxes); + // Create a table and set it as the content of the first pane of the horizontal + // container + var table = CreateTable(win); + efl.Content.static_cast(hsplit.GetPart("first")).SetContent(table); - efl.ui.IPanes horiz_split = new efl.ui.Panes(win); - horiz_split.SetDirection(efl.ui.Dir.Horizontal); - horiz_split.SetSplitRatio(0.85); - efl.Content.static_cast(split.GetPart("second")).SetContent(horiz_split); + // Create a button and set it as the content of the second pane of the horizontal + // container + efl.ui.IButton quit_btn = new efl.ui.Button(win, (efl.ui.IButton ebutton) => { + ebutton.SetText("Quit"); + ebutton.SetHintMax(new eina.Size2D(150, 30)); + ebutton.ClickedEvt += (object sender, EventArgs e) => { + // Exit the EFL main loop + efl.ui.Config.Exit(); + }; + }); + efl.Content.static_cast(hsplit.GetPart("second")).SetContent(quit_btn); - var table = SetupUiTable(win); - efl.Content.static_cast(horiz_split.GetPart("first")).SetContent(table); - - efl.ui.IButton quit_btn = new efl.ui.Button(win); - quit_btn.SetText("Quit"); - sz.W = 150; - sz.H = 30; - quit_btn.SetHintMax(sz); - quit_btn.ClickedEvt += GuiQuitCb; - efl.Content.static_cast(horiz_split.GetPart("second")).SetContent(quit_btn); - - // Start event loop + // Start the EFL main loop efl.ui.Config.Run(); + // Shutdown EFL efl.All.Shutdown(); } } + diff --git a/reference/csharp/ui/src/ui_sizing.cs b/reference/csharp/ui/src/ui_sizing.cs index 2f74b60e..b56cf482 100644 --- a/reference/csharp/ui/src/ui_sizing.cs +++ b/reference/csharp/ui/src/ui_sizing.cs @@ -2,57 +2,62 @@ * Efl.UI sizing examples. * * Demonstrate how to use the sizing api from Efl.Gfx. - * We load a box with 3 buttons, one with default sizing, one that has a max - * and the last has a min size. Try resizing the window to see how this changes. + * We load a box with 3 buttons, one with default sizing, one that has a max size + * and the last one has a min size. Try resizing the window to see how this changes. */ using System; public class Example { +#if WIN32 + [STAThreadAttribute()] +#endif public static void Main() { + // Initialize EFL and all UI components efl.All.Init(efl.Components.Ui); + // Create a window and initialize it efl.ui.IWin win = new efl.ui.Win(null, (efl.ui.IWin ewin) => { ewin.SetWinType(efl.ui.Win_Type.Basic); ewin.SetText("Size Control"); ewin.SetAutohide(true); + ewin.HideEvt += (object sender, EventArgs e) => { + // Exit the EFL main loop + efl.ui.Config.Exit(); + }; }); - eina.Size2D sz; - sz.W = 320; - sz.H = 320; - win.SetSize(sz); - // when the user clicks "close" on a window there is a request to hide - win.HideEvt += (object sender, EventArgs e) => { - // quit the app, called if the window is hidden - efl.ui.Config.Exit(); - }; + // Create a box container + efl.ui.IBox box = new efl.ui.Box(win, (efl.ui.IBox ebox) => { + win.SetContent(ebox); + }); - efl.ui.IBox box = new efl.ui.Box(win); - win.SetContent(box); + // Create a regular button (without size hints) + new efl.ui.Button(win, (efl.ui.IButton ebutton) => { + ebutton.SetText("Button"); + box.DoPack(ebutton); + }); - efl.ui.IButton button = new efl.ui.Button(win); - button.SetText("Button"); - box.DoPack(button); + // Create a small button (max size is limited) + new efl.ui.Button(win, (efl.ui.IButton ebutton) => { + ebutton.SetText("Small"); + ebutton.SetHintMax(new eina.Size2D(50,50)); + box.DoPack(ebutton); + }); - efl.ui.IButton small_btn = new efl.ui.Button(win); - small_btn.SetText("Small"); - sz.W = 50; - sz.H = 50; - small_btn.SetHintMax(sz); - box.DoPack(small_btn); - - efl.ui.IButton big_btn = new efl.ui.Button(win); - big_btn.SetText("Big Button"); - sz.W = 100; - sz.H = 100; - big_btn.SetHintMin(sz); - box.DoPack(big_btn); + // Create a big button (min size is limited) + new efl.ui.Button(win, (efl.ui.IButton ebutton) => { + ebutton.SetText("Big button"); + ebutton.SetHintMin(new eina.Size2D(100,100)); + box.DoPack(ebutton); + }); + // Start the EFL main loop efl.ui.Config.Run(); + // Shutdown EFL efl.All.Shutdown(); } }