Skip to content

Commit

Permalink
Adding UI.ShowMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
Darko Jurić committed Jan 24, 2016
1 parent 963875f commit 6aa43c5
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Deployment/Setup.NuGet/Build.cmd
Expand Up @@ -12,7 +12,7 @@ echo.
timeout /T 5

:: Set version info
set version=4.6.5
set version=4.7.0
set output=%cd%\bin\

:: Create output directory
Expand Down
4 changes: 2 additions & 2 deletions Deployment/Setup.NuGet/UI/UI.nuspec
Expand Up @@ -12,7 +12,7 @@
<iconUrl>https://raw.githubusercontent.com/dajuric/dot-imaging/master/Deployment/Logo/logo-small.png</iconUrl>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>
Portable standalone UI elements invokable as extensions (image display, progress bar, open-save file dialogs, folder-selection dialog, color-picker, mask-drawing dialog, rectangle-drawing dialog).
Portable standalone UI elements invokable as extensions (image display, progress bar, open-save file dialogs, folder-selection dialog, color-picker, mask-drawing dialog, rectangle-drawing dialog, menu dialog).
In order to support other OS platforms (except Windows) install one (or more) of the following packages:

- Eto.Platform.Windows (included by default)
Expand All @@ -24,7 +24,7 @@
- Eto.Platform.XamMac * requires Xamarin Studio on OS X.
- Eto.Platform.XamMac2 * requires Xamarin Studio on OS X.
</description>
<summary>Portable standalone UI elements invokable as extensions (image display, progress bar, open-save file dialogs, folder-selection dialog, color-picker, mask-drawing dialog, rectangle-drawing dialog).</summary>
<summary>Portable standalone UI elements invokable as extensions (image display, progress bar, open-save file dialogs, folder-selection dialog, color-picker, mask-drawing dialog, rectangle-drawing dialog, menu dialog).</summary>
<tags>UI, portable-UI, UI-extensions, DotImaging-UI</tags>

<dependencies>
Expand Down
9 changes: 8 additions & 1 deletion Deployment/Setup.NuGet/UI/readmeUI.txt
@@ -1,4 +1,4 @@
Portable standalone UI elements invokable as extensions (image display, progress bar, open-save file dialogs, folder-selection dialog, color-picker, mask-drawing dialog, rectangle-drawing dialog).
Portable standalone UI elements invokable as extensions (image display, progress bar, open-save file dialogs, folder-selection dialog, color-picker, mask-drawing dialog, rectangle-drawing dialog, menu dialog).

1) image show
Bgr<byte>[,] image = new Bgr<byte>[480, 640];
Expand Down Expand Up @@ -29,6 +29,13 @@
image.SetValue<Bgr<byte>>(Bgr<byte>.Red);
RectangleF rect = image.GetRectangle(); //get user-defined rectangle dialog

7) menu dialog
var selectedNumber = -1;
UI.ShowMenu(itemNames: new string[] { "2", "3" },
actions: new Action[] { () => selectedNumber = 2, () => selectedNumber = 3 });


Console.WriteLine("Selected number: {0}", selectedNumber);

---------------------------------------------------------------------------
In order to support other OS platforms (except Windows) install one (or more) of the following packages:
Expand Down
12 changes: 8 additions & 4 deletions README.md
Expand Up @@ -3,7 +3,7 @@
</p>

<p align="center">
<a href="https://www.nuget.org/profiles/dajuric"> <img src="https://img.shields.io/badge/NuGet-v4.6.5-blue.svg?style=flat-square" alt="NuGet packages version"/> </a>
<a href="https://www.nuget.org/profiles/dajuric"> <img src="https://img.shields.io/badge/NuGet-v4.7-blue.svg?style=flat-square" alt="NuGet packages version"/> </a>
</p>

**DotImaging** - .NET array as imaging object
Expand Down Expand Up @@ -112,11 +112,15 @@ image.Show(); //show image (non-blocking)
string fileName = UI.OpenFile(); //open-file dialog
Bgr<byte> color = UI.PickColor(); //color picker dialog
Bgr<byte> color = UI.PickColor(); //color-picker dialog
Gray<byte>[,] mask = image.GetMask(); //get user-defined mask dialog
Gray<byte>[,] mask = image.GetMask(); //draw-mask dialog
RectangleF rect = image.GetRectangle(); //get user-defined rectangle dialog
RectangleF rect = image.GetRectangle(); //draw-rectangle dialog
var num = -1;
UI.ShowMenu(itemNames: new string[] { "2", "3" },
actions: new Action[] { () => num = 2, () => num = 3 }); //menu-dialog
```

+ <a href="https://www.nuget.org/packages/DotImaging.Linq">DotImaging.Linq</a>
Expand Down
87 changes: 87 additions & 0 deletions Source/UI/Forms/MenuForm.cs
@@ -0,0 +1,87 @@
#region Licence and Terms
// DotImaging Framework
// https://github.com/dajuric/dot-imaging
//
// Copyright © Darko Jurić, 2014-2016
// darko.juric2@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using Eto.Drawing;
using Eto.Forms;
using System;

namespace DotImaging
{
/// <summary>
/// Menu form.
/// </summary>
internal class MenuForm : Form
{
/// <summary>
/// Creates a new menu form.
/// </summary>
/// <param name="title">Window title.</param>
public MenuForm(string title = "")
:this(title, null, null)
{ }

/// <summary>
/// Creates a new menu form.
/// </summary>
/// <param name="title">Window title.</param>
/// <param name="itemNames">Item names.</param>
/// <param name="actions">Actions.</param>
public MenuForm(string title, string[] itemNames, Action[] actions)
{
Title = title;
SelectedIndex = -1;

if (itemNames == null || actions == null)
return;
if (itemNames.Length != actions.Length)
return;

var stackLayout = new StackLayout {Orientation = Orientation.Vertical, HorizontalContentAlignment = HorizontalAlignment.Stretch };

for (int i = 0; i < itemNames.Length; i++)
{
var idx = i;

var button = new Button { Text = itemNames[idx], Size = new Size(240, 60) };
button.Click += (s, e) =>
{
actions[idx]();
SelectedIndex = idx;
this.Close();
};

stackLayout.Items.Add(new StackLayoutItem(button, true));
}

Content = stackLayout;
Size = new Size(-1, -1);
}

/// <summary>
/// Gets the selected menu item index or returns -1 in case the form is closed.
/// </summary>
public int SelectedIndex
{
get; private set;
}
}

}
22 changes: 22 additions & 0 deletions Source/UI/UI.cs
Expand Up @@ -21,6 +21,7 @@

using Eto.Forms;
using DotImaging.Primitives2D;
using System;

namespace DotImaging
{
Expand Down Expand Up @@ -219,6 +220,27 @@ public static RectangleF GetRectangle(this Bgr<byte>[,] image, string windowTitl
return new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
}

/// <summary>
/// Displays a menu dialog specified by user defined menu items.
/// <para>If no items are specified, or in case of a error, the dialog will not be shown.</para>
/// </summary>
/// <param name="windowTitle">Window title (ID).</param>
/// <param name="itemNames">Names of the menu items.</param>
/// <param name="actions">Item actions.</param>
/// <returns>Selected menu index or -1 in case the form is closed.</returns>
public static int ShowMenu(string windowTitle = "Menu (close when finished)", string[] itemNames = null, Action[] actions = null)
{
var idx = FormCollection.CreateAndShowDialog(() =>
{
var f = new MenuForm(windowTitle, itemNames, actions);
f.Title = windowTitle;
return f;
},
f => f.SelectedIndex);

return idx;
}

/// <summary>
/// Closes all UI controls if displayed.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Source/UI/UI.csproj
Expand Up @@ -52,6 +52,7 @@
<Compile Include="FormCollection.cs" />
<Compile Include="Forms\DrawingRectangleForm.cs" />
<Compile Include="Forms\DrawingPenForm.cs" />
<Compile Include="Forms\MenuForm.cs" />
<Compile Include="Forms\ImageForm.cs" />
<Compile Include="Forms\ProgressForm.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
7 changes: 7 additions & 0 deletions Test/Program.cs
Expand Up @@ -38,6 +38,13 @@ class Program
[STAThread]
unsafe static void Main()
{
var selectedIdx = UI.ShowMenu(itemNames: new string[] { "Option A", "Option B" },
actions: new Action[] { () => Console.WriteLine("Option A"), () => Console.WriteLine("Option B") });

Console.WriteLine("Selected option: {0}", selectedIdx);
UI.CloseAll();
return;

var resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources");
var imgColor = ImageIO.LoadColor(Path.Combine(resourceDir, "testColorBig.jpg")).Clone();

Expand Down

0 comments on commit 6aa43c5

Please sign in to comment.