<commit_before>package placebooks.client.ui.elements;

import placebooks.client.PlaceBookService;
import placebooks.client.model.PlaceBook;
import placebooks.client.model.PlaceBookBinder;
import placebooks.client.ui.items.frames.PlaceBookItemFrameFactory;

import com.google.gwt.canvas.dom.client.CanvasGradient;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class PlaceBookPagesBook extends PlaceBookPages
{
	private final static String newPage = "{\"items\":[], \"metadata\":{} }";
	
	private enum FlipState
	{
		none, dragging, edgeHighlight, flipping 
	}
	
	private class Flip extends Timer
	{
		private FlipState state = FlipState.none;
		
		private double target;
		private double progress;
		private PlaceBookPage left;
		private PlaceBookPage right;
		
		@Override
		public void run()
		{
			if(state != FlipState.flipping)
			{
				cancel();
			}
			else
			{
				if(Math.abs(target - progress) > 0.01)
				{	
					drawFlip(left, progress + (target - progress) * 0.2);				
				}
				else
				{
					drawFlip(left, target);
					if(target == 1)
					{
						setPage(left);					
					}
					else if(target == -1)
					{
						setPage(right);
					}
					state = FlipState.none;
					cancel();				
				}
			}
		}	
		
		public void setup(final PlaceBookPage left, final PlaceBookPage right)
		{
			if(left != null)
			{
				left.setVisible(false);
			}
			if(right != null)
			{
				right.setVisible(false);
			}
			
			flip.cancel();
			this.left = left;
			this.right = right;
			left.setVisible(true);
			left.reflow();
			left.getElement().getStyle().setZIndex(1);
			right.setVisible(true);
			right.reflow();
			right.getElement().getStyle().setZIndex(0);
		}
	}
	
	interface PageStyle extends CssResource
	{
		String page();
	}
	
	interface PlaceBookBookPanelUiBinder extends UiBinder<Widget, PlaceBookPagesBook>
	{
	}

	private static PlaceBookBookPanelUiBinder uiBinder = GWT.create(PlaceBookBookPanelUiBinder.class);

	private PlaceBookPage currentPage;

	@UiField
	Panel rootPanel;

	@UiField
	Panel pagesPanel;

	@UiField
	Panel leftPanel;
	
	@UiField
	Canvas canvas;
	
	@UiField
	PageStyle style;
	
	private double bookWidth;
	private double pageWidth;
	private double pageHeight;
	
	private double margin = 30;

	private Flip flip = new Flip();
		
	@Override
	public void setPlaceBook(PlaceBookBinder newPlaceBook, PlaceBookItemFrameFactory factory)
	{
		super.setPlaceBook(newPlaceBook, factory);
		
		if(pages.size() > 0)
		{
			setPage(pages.get(0));
		}
	}

	public PlaceBookPagesBook()
	{
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPage(final PlaceBookPage page)
	{
		GWT.log("Set Page: " + page.getIndex());
		if(currentPage != null)
		{
			currentPage.setVisible(false);			
		}

		currentPage = page;
		currentPage.setVisible(true);		
		currentPage.getElement().getStyle().setZIndex(1);
		currentPage.clearFlip();
	}

	@UiHandler("rootPanel")
	void flipStart(final MouseDownEvent event)
	{
		GWT.log("Flip start");		
		if(pages.size() <= 1)
		{
			return;
		}
		if(flip.state != FlipState.flipping)
		{
			final int mouseX = event.getRelativeX(pagesPanel.getElement());
			// Make sure the mouse pointer is inside of the book
			if (mouseX < pageWidth)
			{		
				if (mouseX < margin && currentPage.getIndex() - 1 >= 0)
				{
					// We are on the left side, drag the previous page
					flip.state = FlipState.dragging;<commit_msg>Set current page at beginning of flip<commit_after>package placebooks.client.ui.elements;

import placebooks.client.PlaceBookService;
import placebooks.client.model.PlaceBook;
import placebooks.client.model.PlaceBookBinder;
import placebooks.client.ui.items.frames.PlaceBookItemFrameFactory;

import com.google.gwt.canvas.dom.client.CanvasGradient;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class PlaceBookPagesBook extends PlaceBookPages
{
	private final static String newPage = "{\"items\":[], \"metadata\":{} }";
	
	private enum FlipState
	{
		none, dragging, edgeHighlight, flipping 
	}
	
	private class Flip extends Timer
	{
		private FlipState state = FlipState.none;
		
		private double target;
		private double progress;
		private PlaceBookPage left;
		private PlaceBookPage right;
		
		@Override
		public void run()
		{
			if(state != FlipState.flipping)
			{
				cancel();
			}
			else
			{
				if(target == 1)
				{
					setPage(left);					
				}
				else if(target == -1)
				{
					setPage(right);
				}
				
				if(Math.abs(target - progress) > 0.01)
				{	
					drawFlip(left, progress + (target - progress) * 0.2);				
				}
				else
				{
					drawFlip(left, target);
					state = FlipState.none;
					cancel();				
				}
			}
		}	
		
		public void setup(final PlaceBookPage left, final PlaceBookPage right)
		{
			if(left != null)
			{
				left.setVisible(false);
			}
			if(right != null)
			{
				right.setVisible(false);
			}
			
			flip.cancel();
			this.left = left;
			this.right = right;
			left.setVisible(true);
			left.reflow();
			left.getElement().getStyle().setZIndex(1);
			right.setVisible(true);
			right.reflow();
			right.getElement().getStyle().setZIndex(0);
		}
	}
	
	interface PageStyle extends CssResource
	{
		String page();
	}
	
	interface PlaceBookBookPanelUiBinder extends UiBinder<Widget, PlaceBookPagesBook>
	{
	}

	private static PlaceBookBookPanelUiBinder uiBinder = GWT.create(PlaceBookBookPanelUiBinder.class);

	private PlaceBookPage currentPage;

	@UiField
	Panel rootPanel;

	@UiField
	Panel pagesPanel;

	@UiField
	Panel leftPanel;
	
	@UiField
	Canvas canvas;
	
	@UiField
	PageStyle style;
	
	private double bookWidth;
	private double pageWidth;
	private double pageHeight;
	
	private double margin = 30;

	private Flip flip = new Flip();
		
	@Override
	public void setPlaceBook(PlaceBookBinder newPlaceBook, PlaceBookItemFrameFactory factory)
	{
		super.setPlaceBook(newPlaceBook, factory);
		
		if(pages.size() > 0)
		{
			setPage(pages.get(0));
		}
	}

	public PlaceBookPagesBook()
	{
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPage(final PlaceBookPage page)
	{
		if(currentPage == page)
		{
			return;
		}
		GWT.log("Set Page: " + page.getIndex());
		if(currentPage != null)
		{
			currentPage.setVisible(false);			
		}

		currentPage = page;
		currentPage.setVisible(true);		
		currentPage.getElement().getStyle().setZIndex(1);
	}

	@UiHandler("rootPanel")
	void flipStart(final MouseDownEvent event)
	{
		GWT.log("Flip start");		
		if(pages.size() <= 1)
		{
			return;
		}
		if(flip.state != FlipState.flipping)
		{
			final int mouseX = event.getRelativeX(pagesPanel.getElement());
			// Make sure the mouse pointer is inside of the book
			if (mouseX < pageWidth)
			{		
				if (mouseX < margin && currentPage.getIndex() - 1 >= 0)
				{
					// We are on the left side, drag the previous page
					flip.state = FlipState.dragging;
{'fraction_non_alphanumeric': 0.11886399804137593, 'fraction_numerical': 0.0048965601664830456, 'mean_word_length': 3.5616973757677277, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746075', 'n_tokens_mistral': 3264, 'n_tokens_neox': 2752, 'n_words': 539}