Skip to content

Commit

Permalink
Project search system (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: jonko0493 <[email protected]>
  • Loading branch information
WiIIiam278 and jonko0493 authored Feb 2, 2023
1 parent 62bc65b commit 519f53c
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 35 deletions.
68 changes: 68 additions & 0 deletions src/SerialLoops/Controls/ItemContextMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using System;

namespace SerialLoops.Controls
{
internal class ItemContextMenu : ContextMenu
{

private readonly ILogger _log;
private readonly Project _project;

private readonly EditorTabsPanel _tabs;
private readonly ItemExplorerPanel _explorer;

public ItemContextMenu(Project project, ItemExplorerPanel explorer, EditorTabsPanel tabs, ILogger log)
{
_project = project;
_tabs = tabs;
_explorer = explorer;
_log = log;

Command openCommand = new();
openCommand.Executed += OpenCommand_OnClick;
Items.Add(new ButtonMenuItem { Text = "Open", Command = openCommand });

Command findReferences = new();
findReferences.Executed += FindReferences_OnClick;
Items.Add(new ButtonMenuItem { Text = "Find References...", Command = findReferences });

Command findReferencedBy = new();
findReferencedBy.Executed += FindReferencedBy_OnClick;
Items.Add(new ButtonMenuItem { Text = "Find Referenced By...", Command = findReferencedBy });
}

private void OpenCommand_OnClick(object sender, EventArgs args)
{
ItemDescription item = _project.FindItem(_explorer.Viewer.SelectedItem?.Text);
if (item != null)
{
_tabs.OpenTab(item, _log);
}
}

private void FindReferences_OnClick(object sender, EventArgs args)
{
ShowReferences(ReferenceDialog.ReferenceMode.REFERENCES_TO);
}

private void FindReferencedBy_OnClick(object sender, EventArgs args)
{
ShowReferences(ReferenceDialog.ReferenceMode.REFERENCED_BY);
}

private void ShowReferences(ReferenceDialog.ReferenceMode mode)
{
ItemDescription item = _project.FindItem(_explorer.Viewer.SelectedItem?.Text);
if (item != null)
{
ReferenceDialog referenceDialog = new(item, mode, _project, _explorer, _tabs, _log);
referenceDialog.ShowModal(_explorer);
}
}

}
}
31 changes: 5 additions & 26 deletions src/SerialLoops/Controls/ItemExplorerPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,23 @@
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace SerialLoops.Controls
{
public class ItemExplorerPanel : Scrollable
public class ItemExplorerPanel : ItemListPanel
{
private ILogger _log;
public static readonly Size ITEM_EXPLORER_BASE_SIZE = new(200, 420);

private SectionListTreeGridView _items;
private readonly Project _project;
private readonly EditorTabsPanel _tabs;

public ItemExplorerPanel(Project project, EditorTabsPanel tabs, ILogger log)
public ItemExplorerPanel(Project project, EditorTabsPanel tabs, ILogger log) : base(project.Items, new Size(200, 420), false, log)
{
_log = log;
_project = project;
_tabs = tabs;
InitializeComponent();
}

void InitializeComponent()
{
MinimumSize = ITEM_EXPLORER_BASE_SIZE;
Padding = 0;

IEnumerable<Section> sections = _project.Items.GroupBy(i => i.Type).OrderBy(g => g.Key)
.Select(g => new Section($"{g.Key}s", g.Select(i => new Section() { Text = i.Name }), EditorTabsPanel.GetItemIcon(g.Key, _log)));

_items = new SectionListTreeGridView(sections, ITEM_EXPLORER_BASE_SIZE);
_items.Activated += ItemList_ItemClicked;

Content = new TableLayout(_items.Control);
((TreeGridView)Viewer.Control).ContextMenu = new ItemContextMenu(project, this, tabs, _log);
}

private void ItemList_ItemClicked(object sender, EventArgs e)
protected override void ItemList_ItemClicked(object sender, EventArgs e)
{
if (sender is SectionListTreeGridView view)
{
Expand Down
59 changes: 59 additions & 0 deletions src/SerialLoops/Controls/ItemListPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Eto.Drawing;
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerialLoops.Controls
{
public abstract class ItemListPanel : Scrollable
{
public List<ItemDescription> Items
{
protected get { return _items; }
set
{
_items = value;
Viewer?.SetContents(GetSections(), _expandItems);
}
}
public SectionListTreeGridView Viewer { get; private set; }

protected ILogger _log;
private readonly Size _size;
private List<ItemDescription> _items;
private readonly bool _expandItems;

protected ItemListPanel(List<ItemDescription> items, Size size, bool expandItems, ILogger log)
{
Items = items;
_log = log;
_size = size;
_expandItems = expandItems;
InitializeComponent();
}

void InitializeComponent()
{
Viewer = new SectionListTreeGridView(GetSections(), _size, _expandItems);
MinimumSize = _size;
Padding = 0;
Content = new TableLayout(Viewer.Control);
Viewer.Activated += ItemList_ItemClicked;
}

private IEnumerable<Section> GetSections()
{
return Items.GroupBy(i => i.Type).OrderBy(g => g.Key)
.Select(g => new Section($"{g.Key}s", g.Select(i => new Section() { Text = i.Name }), EditorTabsPanel.GetItemIcon(g.Key, _log)));
}

protected abstract void ItemList_ItemClicked(object sender, EventArgs e);

}
}
31 changes: 31 additions & 0 deletions src/SerialLoops/Controls/ItemResultsPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Eto.Drawing;
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Lib;
using SerialLoops.Lib.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace SerialLoops.Controls
{
public class ItemResultsPanel : ItemListPanel
{
public FindItemsDialog Dialog;
public ItemResultsPanel(List<ItemDescription> results, ILogger log) : base(results, new Size(280, 185), true, log) { }

protected override void ItemList_ItemClicked(object sender, EventArgs e)
{
if (sender is SectionListTreeGridView view)
{
ItemDescription item = Dialog.Project.FindItem(view.SelectedItem?.Text);
if (item != null)
{
Dialog.Tabs.OpenTab(item, _log);
}
Dialog.Close();
}
}
}
}
15 changes: 10 additions & 5 deletions src/SerialLoops/Controls/SectionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public class SectionTreeItem : List<SectionTreeItem>, ITreeGridItem<SectionTreeI
public bool Expandable => Count > 0;
public ITreeGridItem Parent { get; set; }

public SectionTreeItem(Section section)
public SectionTreeItem(Section section, bool expanded)
{
Section = section;
Expanded = false;
Expanded = expanded;
foreach (var child in section)
{
SectionTreeItem temp = new(child) { Parent = this };
SectionTreeItem temp = new(child, expanded) { Parent = this };
Add(temp); // recursive
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ ITreeGridItem FindItem(SectionTreeItem node, ISection section)
return null;
}

public SectionListTreeGridView(IEnumerable<Section> topNodes, Size size)
public SectionListTreeGridView(IEnumerable<Section> topNodes, Size size, bool expanded)
{
_treeView = new TreeGridView
{
Expand All @@ -176,8 +176,13 @@ public SectionListTreeGridView(IEnumerable<Section> topNodes, Size size)
});
_treeView.SelectedItemChanged += (sender, e) => OnSelectedItemChanged(e);
_treeView.Activated += (sender, e) => OnActivated(e);
_treeView.DataStore = new SectionTreeItem(new Section("Top", topNodes, null));
_treeView.Size = size;
SetContents(topNodes, expanded);
}

public void SetContents(IEnumerable<Section> topNodes, bool expanded)
{
_treeView.DataStore = new SectionTreeItem(new Section("Top", topNodes, null), expanded);
}
}
}
39 changes: 39 additions & 0 deletions src/SerialLoops/FindItemsDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Eto.Forms;
using HaruhiChokuretsuLib.Util;
using SerialLoops.Controls;
using SerialLoops.Lib;
using SerialLoops.Lib.Logging;
using System;

namespace SerialLoops
{
public abstract class FindItemsDialog : Dialog
{
public ILogger Log;
public EditorTabsPanel Tabs;
public ItemExplorerPanel Explorer;
public Project Project;

protected override void OnLoad(EventArgs e)
{
if (Log is null)
{
// We can't log that log is null, so we have to throw
throw new LoggerNullException();
}
if (Project is null)
{
Log.LogError($"Project not provided to project creation dialog");
Close();
}
if (Tabs is null)
{
Log.LogError($"Editor Tabs not provided to project creation dialog");
Close();
}
base.OnLoad(e);
}

}

}
29 changes: 25 additions & 4 deletions src/SerialLoops/MainForm.eto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public partial class MainForm : Form
private LoopyLogger _log;
public Config CurrentConfig { get; set; }
public Project OpenProject { get; set; }
public EditorTabsPanel EditorTabs { get; set; }
public ItemExplorerPanel ItemExplorer { get; set; }

void InitializeComponent()
{
Expand All @@ -30,6 +32,10 @@ void InitializeComponent()
Command openProject = new() { MenuText = "Open Project", ToolBarText = "Open Project" };
openProject.Executed += OpenProject_Executed;

// Tools
Command searchProject = new() { MenuText = "Search", ToolBarText = "Search", Shortcut = Application.Instance.CommonModifier | Keys.F };
searchProject.Executed += Search_Executed;

// About
Command aboutCommand = new() { MenuText = "About..." };
AboutDialog aboutDialog = new() { ProgramName = "Serial Loops", Developers = new string[] { "Jonko", "William" }, Copyright = "© Haroohie Translation Club, 2023", Website = new Uri("https://haroohie.club") };
Expand All @@ -42,9 +48,10 @@ void InitializeComponent()
{
// File submenu
new SubMenuItem { Text = "&File", Items = { newProject, openProject } },
new SubMenuItem { Text = "&Tools", Items = { searchProject } },
// new SubMenuItem { Text = "&Edit", Items = { /* commands/items */ } },
// new SubMenuItem { Text = "&View", Items = { /* commands/items */ } },
new SubMenuItem { Text = "&Build", Items = { } },
//new SubMenuItem { Text = "&Build", Items = { } },
},
ApplicationItems =
{
Expand All @@ -57,10 +64,10 @@ void InitializeComponent()

private void OpenProjectView(Project project)
{
EditorTabsPanel tabs = new(project);
ItemExplorerPanel items = new(project, tabs, _log);
EditorTabs = new(project);
ItemExplorer = new(project, EditorTabs, _log);
Title = $"{BASE_TITLE} - {project.Name}";
Content = new TableLayout(new TableRow(items, tabs));
Content = new TableLayout(new TableRow(ItemExplorer, EditorTabs));
}

protected override void OnLoad(EventArgs e)
Expand Down Expand Up @@ -91,5 +98,19 @@ private void OpenProject_Executed(object sender, EventArgs e)
OpenProjectView(OpenProject);
}
}

private void Search_Executed(object sender, EventArgs e)
{
if (OpenProject is not null)
{
SearchDialog searchDialog = new()
{
Project = OpenProject,
Tabs = EditorTabs,
Log = _log
};
searchDialog.ShowModal(this);
}
}
}
}
15 changes: 15 additions & 0 deletions src/SerialLoops/ReferenceDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Eto.Forms;
using Eto.Drawing;
using System.Collections.Generic;

namespace SerialLoops
{
public partial class ReferenceDialog : FindItemsDialog
{
public ReferenceDialog()
{
InitializeComponent();
}
}
}
Loading

0 comments on commit 519f53c

Please sign in to comment.