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
This commit is contained in:
Xavi Artigas 2018-09-17 14:18:06 -03:00 committed by Vitor Sousa
parent 44155d8827
commit fce18f2ebe
2 changed files with 125 additions and 104 deletions

View File

@ -1,112 +1,128 @@
/* /*
* Efl.UI container exmaples. * Efl.UI container examples.
* *
* Load and pack a selection of containers. * 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; using System;
public class Example public class Example
{ {
// quit the app, called if the user clicks the Quit button or the window is hidden // Create a box container full of buttons
static void GuiQuitCb(object sender, EventArgs e) static efl.ui.IBox CreateBox(efl.ui.IWin win)
{ {
efl.ui.Config.Exit(); efl.ui.IBox box = new efl.ui.Box(win, (efl.ui.IBox ebox) => {
} // Set distance between contained elements
ebox.SetPackPadding(5, 0, true);
// 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);
for (int i = 1; i <= 4; ++i) for (int i = 1; i <= 4; ++i)
{ {
efl.ui.IButton btn = new efl.ui.Button(win); // Add 4 buttons, one below the other
btn.SetText($"Table {i}"); new efl.ui.Button(win, (efl.ui.IButton ebutton) => {
table.DoPack(btn); ebutton.SetText($"Boxed {i}");
} if (i == 2)
{
return table; // Button 2 has its maximum size limited, so it will be smaller
} ebutton.SetHintMax(new eina.Size2D(100,50));
}
// Load some boxes - a horizontal one for the window layout and a vertical box.DoPack(ebutton);
// 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);
} }
return box; 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() public static void Main()
{ {
// Initialize EFL and all UI components
efl.All.Init(efl.Components.Ui); 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) => { efl.ui.IWin win = new efl.ui.Win(null, (efl.ui.IWin ewin) => {
ewin.SetWinType(efl.ui.Win_Type.Basic); ewin.SetWinType(efl.ui.Win_Type.Basic);
ewin.SetText("Hello World"); ewin.SetText("Container demo");
ewin.SetAutohide(true); ewin.SetAutohide(true);
ewin.HideEvt += (object sender, EventArgs e) => {
// Exit the EFL main loop
efl.ui.Config.Exit();
};
}); });
eina.Size2D sz; // Give the window an initial size so there is room to resize the panes.
sz.W = 350; // Otherwise, all widgets are tightly packed
sz.H = 250; win.SetSize(new eina.Size2D(350,250));
win.SetSize(sz);
// when the user clicks "close" on a window there is a request to hide // Create a vertically-split panes container
win.HideEvt += GuiQuitCb; 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); // Create a second, horizontally-split panes container and set it as the content of
split.SetSplitRatio(0.75); // the second pane of the first container
win.SetContent(split); 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); // Create a table and set it as the content of the first pane of the horizontal
efl.Content.static_cast(split.GetPart("first")).SetContent(boxes); // container
var table = CreateTable(win);
efl.Content.static_cast(hsplit.GetPart("first")).SetContent(table);
efl.ui.IPanes horiz_split = new efl.ui.Panes(win); // Create a button and set it as the content of the second pane of the horizontal
horiz_split.SetDirection(efl.ui.Dir.Horizontal); // container
horiz_split.SetSplitRatio(0.85); efl.ui.IButton quit_btn = new efl.ui.Button(win, (efl.ui.IButton ebutton) => {
efl.Content.static_cast(split.GetPart("second")).SetContent(horiz_split); 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); // Start the EFL main loop
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
efl.ui.Config.Run(); efl.ui.Config.Run();
// Shutdown EFL
efl.All.Shutdown(); efl.All.Shutdown();
} }
} }

View File

@ -2,57 +2,62 @@
* Efl.UI sizing examples. * Efl.UI sizing examples.
* *
* Demonstrate how to use the sizing api from Efl.Gfx. * 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 * We load a box with 3 buttons, one with default sizing, one that has a max size
* and the last has a min size. Try resizing the window to see how this changes. * and the last one has a min size. Try resizing the window to see how this changes.
*/ */
using System; using System;
public class Example public class Example
{ {
#if WIN32
[STAThreadAttribute()]
#endif
public static void Main() public static void Main()
{ {
// Initialize EFL and all UI components
efl.All.Init(efl.Components.Ui); 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) => { efl.ui.IWin win = new efl.ui.Win(null, (efl.ui.IWin ewin) => {
ewin.SetWinType(efl.ui.Win_Type.Basic); ewin.SetWinType(efl.ui.Win_Type.Basic);
ewin.SetText("Size Control"); ewin.SetText("Size Control");
ewin.SetAutohide(true); 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 // Create a box container
win.HideEvt += (object sender, EventArgs e) => { efl.ui.IBox box = new efl.ui.Box(win, (efl.ui.IBox ebox) => {
// quit the app, called if the window is hidden win.SetContent(ebox);
efl.ui.Config.Exit(); });
};
efl.ui.IBox box = new efl.ui.Box(win); // Create a regular button (without size hints)
win.SetContent(box); new efl.ui.Button(win, (efl.ui.IButton ebutton) => {
ebutton.SetText("Button");
box.DoPack(ebutton);
});
efl.ui.IButton button = new efl.ui.Button(win); // Create a small button (max size is limited)
button.SetText("Button"); new efl.ui.Button(win, (efl.ui.IButton ebutton) => {
box.DoPack(button); ebutton.SetText("Small");
ebutton.SetHintMax(new eina.Size2D(50,50));
box.DoPack(ebutton);
});
efl.ui.IButton small_btn = new efl.ui.Button(win); // Create a big button (min size is limited)
small_btn.SetText("Small"); new efl.ui.Button(win, (efl.ui.IButton ebutton) => {
sz.W = 50; ebutton.SetText("Big button");
sz.H = 50; ebutton.SetHintMin(new eina.Size2D(100,100));
small_btn.SetHintMax(sz); box.DoPack(ebutton);
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);
// Start the EFL main loop
efl.ui.Config.Run(); efl.ui.Config.Run();
// Shutdown EFL
efl.All.Shutdown(); efl.All.Shutdown();
} }
} }