Skip to content

Commit

Permalink
Finished a rough implementation of file chooser visuals
Browse files Browse the repository at this point in the history
  • Loading branch information
ktvoelker committed Mar 7, 2011
1 parent d66c893 commit cd0779b
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 80 deletions.
10 changes: 7 additions & 3 deletions Di/Controller/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,15 @@ public override void Execute(Window b)
FileChooser chooser = null;
Action<Di.Model.ProjectFile> handler = file =>
{
b.Controller.CallEndFileChooser(chooser);
b.Controller.EndFileChooser.Handler(chooser);
b.Controller.FocusedWindow.Value = b.Controller.CreateWindow(file);
};
chooser = new FileChooser(b.Controller.Model.CurrentProject, handler);
b.Controller.CallBeginFileChooser(chooser);
Action cancel = () =>
{
b.Controller.CancelFileChooser.Handler();
};
chooser = new FileChooser(b.Controller.Model.CurrentProject, handler, cancel);
b.Controller.BeginFileChooser.Handler(chooser);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion Di/Controller/FileChooser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class FileChooser

private Action<Di.Model.ProjectFile> handler;

private Action cancelHandler;

private Di.Model.FileQuery query;

public string Query
Expand All @@ -42,10 +44,11 @@ public string Query

public BindList<Di.Model.ProjectFile> Files;

public FileChooser(Di.Model.Project _project, Action<Di.Model.ProjectFile> _handler)
public FileChooser(Di.Model.Project _project, Action<Di.Model.ProjectFile> _handler, Action _cancelHandler)
{
project = _project;
handler = _handler;
cancelHandler = _cancelHandler;
query = new Di.Model.FileQuery("");
Files = new BindList<Di.Model.ProjectFile>();
Update();
Expand All @@ -56,6 +59,11 @@ public void Choose(Di.Model.ProjectFile file)
handler(file);
}

public void Cancel()
{
cancelHandler();
}

private void Update()
{
query.Evaluate(project.Files).ForEach(f => Files.Add(f));
Expand Down
42 changes: 3 additions & 39 deletions Di/Controller/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,11 @@ public partial class Main

public readonly ReadOnlyCollection<WindowMode> WindowModes;

public delegate void FileChooserHandler(FileChooser c);
public Event1<FileChooser> BeginFileChooser = new Event1<FileChooser>();

private FileChooserHandler beginFileChooser = (c) => { return; };
public Event1<FileChooser> EndFileChooser = new Event1<FileChooser>();

public event FileChooserHandler BeginFileChooser
{
add
{
beginFileChooser += value;
}

remove
{
beginFileChooser -= value;
}
}

public void CallBeginFileChooser(FileChooser c)
{
beginFileChooser(c);
}

private FileChooserHandler endFileChooser = (c) => { return; };

public event FileChooserHandler EndFileChooser
{
add
{
endFileChooser += value;
}

remove
{
beginFileChooser -= value;
}
}

public void CallEndFileChooser(FileChooser c)
{
endFileChooser(c);
}
public Event0 CancelFileChooser = new Event0();

public Main(Model.Main m)
{
Expand Down
6 changes: 6 additions & 0 deletions Di/Di.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<ConsolePause>false</ConsolePause>
<EnvironmentVariables>
<EnvironmentVariables>
<Variable name="DI_PROJECT" value="/home/karl/code/test-project" />
</EnvironmentVariables>
</EnvironmentVariables>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
Expand Down Expand Up @@ -61,6 +66,7 @@
<Compile Include="Model\Language\Base.cs" />
<Compile Include="Model\FileQuery.cs" />
<Compile Include="Controller\FileChooser.cs" />
<Compile Include="Event.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
Expand Down
91 changes: 91 additions & 0 deletions Di/Event.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Event.cs
//
// Author:
// Karl Voelker <[email protected]>
//
// Copyright (c) 2011 Karl Voelker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace Di
{
public abstract class Event<T>
{
public T Handler
{
get;
protected set;
}

public abstract void Add(T f);

public abstract void Remove(T f);
}

public class Event0 : Event<Action>
{
public Event0()
{
Handler = () => { return; };
}

public override void Add(Action f)
{
Handler += f;
}

public override void Remove(Action f)
{
Handler -= f;
}
}

public class Event1<T> : Event<Action<T>>
{
public Event1()
{
Handler = (x) => { return; };
}

public override void Add(Action<T> f)
{
Handler += f;
}

public override void Remove(Action<T> f)
{
Handler -= f;
}
}

public class Event2<T, U> : Event<Action<T, U>>
{
public Event2()
{
Handler = (x, y) => { return; };
}

public override void Add(Action<T, U> f)
{
Handler += f;
}

public override void Remove(Action<T, U> f)
{
Handler -= f;
}
}
}

37 changes: 26 additions & 11 deletions Di/FileMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,33 @@ public FileMatcher()
public void IncludeGlob(string glob)
{
include = null;
includePattern = RegexUnion(includePattern, glob);
includePattern = RegexUnion(includePattern, GlobToRegex(glob));
}

public void ExcludeGlob(string glob)
{
exclude = null;
excludePattern = RegexUnion(excludePattern, glob);
excludePattern = RegexUnion(excludePattern, GlobToRegex(glob));
}

private bool Match(string path)
private bool MatchDefaultInclude(string path)
{
return (Exclude != null && !Exclude.IsMatch(path)) || (Include != null && Include.IsMatch(path));
return Exclude == null || !Exclude.IsMatch(path) || (Include != null && Include.IsMatch(path));
}

private bool MatchDefaultExclude(string path)
{
return Include != null && Include.IsMatch(path) && (Exclude == null || !Exclude.IsMatch(path));
}

private bool MatchDir(DirectoryInfo dir)
{
return MatchDefaultInclude(dir.FullName + Path.DirectorySeparatorChar);
}

private bool MatchFile(FileInfo file)
{
return MatchDefaultExclude(file.FullName);
}

public IList<FileInfo> MatchAll(DirectoryInfo root)
Expand All @@ -95,15 +110,15 @@ public IList<FileInfo> MatchAll(DirectoryInfo root)

private void MatchAll(DirectoryInfo dir, ref IList<FileInfo> matches)
{
if (Match(dir.FullName + Path.PathSeparator))
if (MatchDir(dir))
{
foreach (var subdir in dir.GetDirectories())
{
MatchAll(subdir, ref matches);
}
foreach (var file in dir.GetFiles())
{
if (Match(file.FullName))
if (MatchFile(file))
{
matches.Add(file);
}
Expand Down Expand Up @@ -169,13 +184,13 @@ private static string RegexUnion(IEnumerable<string> xs)
//
// When the regex is being matched against a directory, the path separator should be
// appended to the name.
private static Regex GlobToRegex(string glob)
private static string GlobToRegex(string glob)
{
if (glob[0] == Path.PathSeparator)
if (glob[0] == Path.DirectorySeparatorChar)
{
throw new ArgumentException("A glob may not start with a path separator.");
}
string pathSeparator = Escape(Path.PathSeparator);
string pathSeparator = Escape(Path.DirectorySeparatorChar);
string notPathSeparator = "[^" + pathSeparator + "]";
var pat = new StringBuilder();
// The match must begin at the beginning of the string or at a path separator.
Expand All @@ -200,7 +215,7 @@ private static Regex GlobToRegex(string glob)
{
pat.Append('|');
}
if (m == Path.PathSeparator)
if (m == Path.DirectorySeparatorChar)
{
throw new ArgumentException("A glob may not have a character class containing the path separator.");
}
Expand Down Expand Up @@ -235,7 +250,7 @@ private static Regex GlobToRegex(string glob)
throw new ArgumentException("A glob may not contain dangling character classes.");
}
pat.Append('$');
return new Regex(pat.ToString());
return pat.ToString();
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions Di/Model/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
namespace Di.Model
{
public class Main
Expand All @@ -30,9 +31,9 @@ public class Main

public Project CurrentProject { get; private set; }

public Main()
public Main(DirectoryInfo root)
{
CurrentProject = new Project();
CurrentProject = new Project(root);

buffers = new BindList<Buffer>();
buffers.Add(new Buffer());
Expand Down
4 changes: 0 additions & 4 deletions Di/Model/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ public string Name

public ReadOnlyCollection<ProjectFile> Files { get; private set; }

public Project() : this(new DirectoryInfo(Environment.CurrentDirectory))
{
}

public Project(DirectoryInfo _dir)
{
while (!DirIsProjectRoot(_dir))
Expand Down
20 changes: 19 additions & 1 deletion Di/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using Gtk;

namespace Di
Expand All @@ -27,8 +28,25 @@ class Program
{
public static void Main(string[] args)
{
string rootPath;
if (args.Length == 0)
{
string env = Environment.GetEnvironmentVariable("DI_PROJECT");
if (string.IsNullOrEmpty(env))
{
rootPath = Environment.CurrentDirectory;
}
else
{
rootPath = env;
}
}
else
{
rootPath = args[0];
}
Application.Init();
new View.Main(new Controller.Main(new Model.Main())).ShowAll();
new View.Main(new Controller.Main(new Model.Main(new DirectoryInfo(rootPath)))).ShowAll();
Application.Run();
}
}
Expand Down
12 changes: 11 additions & 1 deletion Di/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
TODO
====

Implement View.FileChooserView based on the comments in that file
Improve the layout of the FileChooserView

Replace the excessLabel with a statusbar

Maintain a fixed width for the whole widget

Use a fixed-width font in the entry box and result list

Only include the part of the path starting from the project root

This should be implemented in ProjectFile as a string property

Add support for using the file chooser to pick a directory
Make Project expose a separate list of all directories
Expand Down
Loading

0 comments on commit cd0779b

Please sign in to comment.