PlotDirector/PlotDirector.NativeCompanion/Vsto/CompanionSettingsWindow.cs

103 lines
8.1 KiB
C#

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using PlotDirector.Companion.Protocol;
namespace PlotDirector.WordAddIn
{
public sealed class BookSelectionWindow : Form
{
private readonly WordAdapter adapter;
private readonly Guid session;
private Guid authentication;
private readonly TextBox search=new TextBox{Dock=DockStyle.Fill};
private readonly ListBox books=new ListBox{Dock=DockStyle.Fill,DisplayMember="Title"};
private readonly Label status=new Label{AutoSize=true,MaximumSize=new Size(620,0)};
private readonly Button confirm=new Button{Text="Connect this manuscript",AutoSize=true};
private readonly CheckBox remember=new CheckBox{Text="Remember this book in the document",AutoSize=true};
private readonly Timer refresh=new Timer{Interval=500};
private string bookKey="";
private int selected;
public BookSelectionWindow(WordAdapter adapter,int selected)
{
this.adapter=adapter;session=adapter.ActiveSession;authentication=adapter.AuthenticationContext;this.selected=selected;
Text="Select PlotDirector book";Font=new Font("Segoe UI",10);AutoScaleMode=AutoScaleMode.Dpi;
Width=700;Height=580;MinimumSize=new Size(540,470);
var layout=new TableLayoutPanel{Dock=DockStyle.Fill,Padding=new Padding(16),ColumnCount=1,RowCount=7};
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent,100));
for(var i=0;i<7;i++)layout.RowStyles.Add(new RowStyle(i==3?SizeType.Percent:SizeType.AutoSize,i==3?100:0));
layout.Controls.Add(new Label{Text="Choose the PlotDirector book for "+adapter.DocumentName,AutoSize=true},0,0);
layout.Controls.Add(status,0,1);
var searchLayout=new TableLayoutPanel{Dock=DockStyle.Fill,AutoSize=true,ColumnCount=1,RowCount=2,Margin=new Padding(0,10,0,6)};
searchLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent,100));
searchLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));searchLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
search.AccessibleName="Search books by title";
searchLayout.Controls.Add(new Label{Text="Search books by title (leave empty to show all)",AutoSize=true},0,0);
searchLayout.Controls.Add(search,0,1);layout.Controls.Add(searchLayout,0,2);
layout.Controls.Add(books,0,3);layout.Controls.Add(remember,0,4);
layout.Controls.Add(new Label{Text="Search by book title above. Connecting a book does not upload manuscript text.\nRemember stores the book, site and document identifier in one custom property. It marks the document modified. Save normally to keep it after reopening. Leaving Remember unchecked keeps the selection for this session and preserves any previously saved association.",AutoSize=true,MaximumSize=new Size(620,0),Margin=new Padding(0,8,0,8)},0,5);
var actions=new FlowLayoutPanel{AutoSize=true,Dock=DockStyle.Fill};
var check=new Button{Text="Refresh connection",AutoSize=true};check.Click+=(_,__)=>adapter.CheckConnection();
var connect=new Button{Text="Sign in",AutoSize=true};connect.Click+=(_,__)=>new ConnectWindow(adapter).Show();
var cancel=new Button{Text="Cancel",AutoSize=true};cancel.Click+=(_,__)=>Close();
confirm.Click+=(_,__)=>{
if(books.SelectedItem is CompanionBook book&&adapter.ConfirmBookSelection(session,authentication,book.BookId,remember.Checked))Close();
};
actions.Controls.AddRange(new Control[]{confirm,check,connect,cancel});layout.Controls.Add(actions,0,6);Controls.Add(layout);
search.TextChanged+=(_,__)=>{bookKey="";Render();};books.SelectedIndexChanged+=(_,__)=>{if(books.SelectedItem is CompanionBook book)selected=book.BookId;};
refresh.Tick+=(_,__)=>Render();refresh.Start();Render();
}
private void Render()
{
if(adapter.ActiveSession!=session){status.Text="The active manuscript changed. Close this window and select a book for that document.";confirm.Enabled=false;return;}
if(authentication!=adapter.AuthenticationContext){authentication=adapter.AuthenticationContext;selected=0;bookKey="";}
var available=adapter.AccessibleBooks??Array.Empty<CompanionBook>();
var filtered=available.Where(b=>(b.Title??"").IndexOf(search.Text.Trim(),StringComparison.CurrentCultureIgnoreCase)>=0).OrderBy(b=>b.Title).ToArray();
var key=authentication+"|"+string.Join("|",filtered.Select(b=>b.BookId+":"+b.Title));
if(key!=bookKey)
{
bookKey=key;var previous=selected;books.DataSource=filtered;
books.SelectedIndex=Array.FindIndex(filtered,b=>b.BookId==previous);
selected=previous;
}
confirm.Enabled=adapter.CanSelectBook&&books.SelectedItem is CompanionBook;
status.Text=adapter.CanSelectBook?available.Length+" accessible books. Current book: "+adapter.BookTitle+(available.Length>0&&filtered.Length==0?". No books match your search; clear the search field to show all.":""):
"Sign in and refresh connection to load books you can access. "+adapter.CloudState;
}
protected override void Dispose(bool disposing){if(disposing)refresh.Dispose();base.Dispose(disposing);}
}
public sealed class CompanionSettingsWindow : Form
{
private readonly WordAdapter adapter;
private readonly TextBox summary=new TextBox{Multiline=true,ReadOnly=true,Dock=DockStyle.Fill,ScrollBars=ScrollBars.Vertical};
private readonly Timer refresh=new Timer{Interval=1000};
public CompanionSettingsWindow(WordAdapter adapter)
{
this.adapter=adapter;Text="PlotDirector settings";Font=new Font("Segoe UI",10);AutoScaleMode=AutoScaleMode.Dpi;
Width=720;Height=560;MinimumSize=new Size(560,430);
var layout=new TableLayoutPanel{Dock=DockStyle.Fill,Padding=new Padding(16),ColumnCount=1,RowCount=2};
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent,100));layout.RowStyles.Add(new RowStyle(SizeType.Percent,100));layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));layout.Controls.Add(summary,0,0);
var actions=new FlowLayoutPanel{AutoSize=true,Dock=DockStyle.Fill,WrapContents=true};
Add(actions,"Sign in / account",()=>new ConnectWindow(adapter).Show());
Add(actions,"Select / change book",()=>adapter.ShowBookSelection());
Add(actions,"Check connection",adapter.CheckConnection);
Add(actions,"Reconnect local agent",adapter.Reconnect);
Add(actions,"Sign out",adapter.SignOut);
Add(actions,"Close",Close);
layout.Controls.Add(actions,0,1);Controls.Add(layout);refresh.Tick+=(_,__)=>Render();refresh.Start();Render();
}
private static void Add(FlowLayoutPanel panel,string text,Action action){var button=new Button{Text=text,AutoSize=true};button.Click+=(_,__)=>action();panel.Controls.Add(button);}
private void Render()
{
var text="ACCOUNT AND CONNECTION\r\n"+adapter.ServerUrl+"\r\n"+adapter.CloudState+"\r\nLocal agent: "+adapter.ConnectionState+
"\r\n\r\nCURRENT MANUSCRIPT\r\n"+adapter.DocumentName+"\r\nBook: "+adapter.BookTitle+"\r\n"+adapter.LocationSummary+"\r\n"+adapter.WordCountSummary+
"\r\n\r\nREMEMBERING THIS BOOK\r\n"+adapter.AssociationSummary+
"\r\n\r\nANALYSIS AND SAVED INFORMATION\r\nReview structure detects chapters and scenes locally. Double-click a result to navigate. AI chapter review requires a separate explicit upload confirmation. Context and Intent show saved PlotDirector information for authorised markers; unmatched structure needs review.\r\n\r\nAutomatic prose synchronisation is unavailable. Book selection does not upload text or import records. Open PlotDirector to view this book in your browser. Views opened here stay on the chosen book.";
if(summary.Text!=text)summary.Text=text;
}
protected override void Dispose(bool disposing){if(disposing)refresh.Dispose();base.Dispose(disposing);}
}
}